Sliding window coding interview questions
Fixed-size sliding window
The window has length k. Each step adds the new right element and removes the one that left.
1.1 Maximum Average Subarray I Easy 3 min
I keep a running sum of a window of length k. I start with the first k elements, then slide one step at a time by adding the new right value and subtracting the value that left on the left. I track the maximum sum and divide by k at the end. Linear time, constant extra space.
Read the full answer1.2 Find All Anagrams in a String Medium 3 min
An anagram of p is any substring of s that uses the same letters with the same counts. I slide a window of length p over s, keep a count array of 26 letters, and whenever the window counts match p's counts I record the start index. Adding the new right character and removing the left one updates the window in constant time.
Read the full answer1.3 Permutation in String Medium 3 min
I need to know whether any permutation of s1 appears as a substring of s2. That is the same fixed window as Find All Anagrams, except I return true at the first frequency match and false if I reach the end without one.
Read the full answer1.4 Fraudulent Activity Notifications Medium 4 min
For each day after a trailing window of d days, the bank sends a notice when that day's spend is at least twice the median of the window. Official constraints cap each spend at 200, so I keep a counting array of frequencies, look up the median in linear time over that small range, then slide the window by incrementing today and decrementing the day that just aged out.
Read the full answer
Variable-size sliding window
Grow on the right, shrink from the left while a monotonic condition holds, and record a longest, shortest or counted answer.
2.1 Longest Substring Without Repeating Characters Medium 3 min
I grow a window over the string and store the last index of each character. When the character at the right edge was already inside the window, I move the left edge to one past that earlier index. The answer is the longest window I see. Linear time and space proportional to the alphabet.
Read the full answer2.2 Fruit Into Baskets Medium 3 min
The problem is the longest subarray that contains at most two distinct values. I grow a window and count the fruits inside it. While more than two types are in the window I drop from the left, and I keep the longest valid length. One pass, and the map holds at most three keys.
Read the full answer2.3 Longest Repeating Character Replacement Medium 3 min
I want the longest substring that can become one repeated letter after at most k replacements. In a window that is valid when length minus the count of its most frequent letter is at most k, I can fill the rest of the window with that letter. I grow the window, track the best letter frequency, shrink from the left when the window needs more than k changes, and keep the longest valid length.
Read the full answer2.4 Max Consecutive Ones III Medium 3 min
I may flip at most k zeros to ones, and I want the longest run of ones I can make. That is a variable window that may contain at most k zeros. I grow the window, count zeros, shrink from the left when the count exceeds k, and keep the longest valid length.
Read the full answer2.5 Minimum Size Subarray Sum Medium 3 min
I need the shortest subarray whose sum is at least the target. Because all values are positive, growing the window increases the sum and shrinking decreases it, so a two-pointer window works. I add on the right, then shrink from the left while the sum stays large enough, and I keep the shortest length I see. If no window qualifies I return 0.
Read the full answer2.6 Minimum Window Substring Hard 4 min
I need the shortest substring of s that covers every character of t, including duplicates. I grow a window until it satisfies the required counts, then shrink from the left as far as it still does, recording the shortest window I see. If nothing covers t I return an empty string.
Read the full answer2.7 Subarrays with K Different Integers Hard 3 min
The number of subarrays with exactly k distinct values is the number with at most k distinct, minus the number with at most k minus one. 'At most k' is a standard variable window: I grow, shrink from the left when the distinct count exceeds the limit, and add the number of valid endings at the current right edge, which is the window length.
Read the full answer