Sliding Window Problems for Coding Interviews
Sliding window turns many O(n squared) subarray and substring problems into O(n). Keep two pointers that mark a window, grow it on the right, and shrink it from the left while a condition is broken. There are two common shapes: a fixed-size window (maximum sum of k elements) and a variable-size window (longest substring without repeating characters).
To recognise it, look for words like longest, shortest, subarray, substring, contiguous, at most k. Write down what the window must satisfy and what you update when it moves, then code the loop. The problems below are tagged with this pattern and run online.
Coding problems
- Maximum Subarray (Intermediate)
- Minimum Size Subarray Sum (Intermediate)
- Subarray Sum Equals K (Intermediate)
- Maximum Sum of a Window (Basic)
- Maximum Vowels in a Window (Intermediate)
- Sum of Subarray Minimums (Advanced)
- Sum of Odd-Length Subarrays (Intermediate)
- Maximum Subarray After One Deletion (Advanced)
- Count Subarrays With K Odd Numbers (Intermediate)
- Subarrays Divisible by K (Intermediate)
- Shortest Unsorted Middle Part (Intermediate)
- Subarrays With Product Less Than K (Intermediate)
- Longest Mountain Subarray (Intermediate)
- Binary Subarrays With Target Sum (Intermediate)
- Arithmetic Slices (Intermediate)
- Circular Window Maximum (Basic)
- Longest Common Contiguous Segment (Advanced)
- Longest XOR Subarray (Intermediate)
- Count Palindromic Substrings (Intermediate)
- Maximum Sum of Distinct Window (Intermediate)
- Count Subarrays With Fixed Bounds (Intermediate)
- Longest Equal Zeros and Ones Subarray (Advanced)
- Maximum Non-Overlapping Target Segments (Advanced)
- Maximum Product Subarray (Intermediate)
- Subarray Sum Equals K (Intermediate)
- Maximum Circular Subarray (Intermediate)
- Shortest Unsorted Subarray Length (Intermediate)
- Minimum Window Substring (Advanced)
- Sum of Subarray Minimums (Advanced)
- Maximum Sum Subarray of Length K (Basic)
- Maximum Length Subarray With Sum Zero (Intermediate)
- Longest Bounded-Difference Segment (Intermediate)
- Median of Sliding Windows (Intermediate)
- Minimum Operations to Sort by Reversals (Intermediate)
- Maximum Subarray With Updates (Intermediate)
- Count Substrings With Exactly K Distinct (Intermediate)
- Minimum Window With All Vowels (Intermediate)
- Count Subarrays With Sum Multiple of K (Intermediate)
- Longest Subarray With Sum At Most K (Advanced)
- Minimum Length Subarray With Sum At Least K (Intermediate)
- Count Subarrays With Median K (Basic)
- Longest Subarray With Bounded Maximum (Intermediate)
- Maximum of Minimum for Every Window Size (Intermediate)
- Maximum Sum of K Disjoint Segments (Basic)
- Longest Turbulent Segment (Intermediate)
- Rate Limiter Sliding Log (Basic)
- Event Time Window Join (Basic)
- Session Window Merge (Intermediate)
- Rolling Median With Late Events (Intermediate)
- Rolling Correlation (Intermediate)
- Traveling Salesperson With Time Windows (Advanced)
- Strange Printer (Advanced)
- Three Non-Overlapping Subarrays (Advanced)
- Ways to Split Array Into Good Subarrays (Intermediate)
- Sum of Subarray Ranges (Intermediate)
- Minimum Increments to Form Target Array (Advanced)
- Degree of an Array (Basic)
- Sliding Window Maximum Values (Advanced)
- Subarrays Where Max Appears at Least K Times (Intermediate)
- Shortest Subarray to Remove to Make Sorted (Advanced)
- Make Two Arrays Equal by Reversing Subarrays (Basic)
- Maximum Ascending Subarray Sum (Basic)
- Sublist Search (Intermediate)
- Palindrome Removal (Advanced)
- Minimum Window Subsequence (Advanced)
- At Most One Segment of Ones (Basic)
- Three Parts With Equal Sum (Basic)
- Subarrays of Size K With Average at Least Threshold (Basic)
- Maximum Erasure Value (Intermediate)
- Longest Nice Subarray (Intermediate)
- Count Complete Subarrays (Intermediate)
- Shortest Subarray With OR at Least K (Intermediate)
- Longest Equal Subarray After Deletions (Intermediate)
- Sliding Subarray Beauty (Intermediate)
- K Radius Subarray Averages (Intermediate)
- Power of K-Size Subarrays (Intermediate)
- XOR Queries of a Subarray (Intermediate)
- Subarrays With Odd Sum (Intermediate)
- Make Sum Divisible by P (Advanced)
- Two Non-Overlapping Subarrays With Target Sum (Intermediate)
Related
Frequently asked questions
When should I use a sliding window?
When you need the best contiguous subarray or substring under a condition, and extending or shrinking the range changes the answer in a predictable way.
What is the difference between fixed and variable windows?
A fixed window has a set size and slides one step at a time. A variable window grows until a condition breaks, then shrinks from the left until it holds again.
What is the time complexity?
Usually O(n), because each pointer moves forward at most n times, plus any cost of the structure you keep for the window, such as a hash map.