Skip to content
TOPIC · 12 PROBLEMS · 5 PATTERNS

Sliding window

A stretch of the array that grows on the right and shrinks on the left. You update what you know about it one item at a time instead of starting each slice from scratch.

How to spot a sliding window problem

“substring”A contiguous run of characters or numbers. If it has to be unbroken, a window can slide over it.
“longest”The longest run that obeys a rule. Grow while the rule holds, shrink from the left when it breaks.
“size k”A fixed length is the simplest window. Add the item coming in, remove the one going out.
“at most”A limit on distinct values, repeats or changes. That limit is what decides when the window must shrink.

Five patterns cover almost everything

Longest valid window

O(n)

Grow on the right every step. When the window breaks the rule, shrink from the left until it is valid again, then record its size.

Reach for it when you want the longest run that stays within some limit.

Shortest valid window

O(n)

Grow until the window satisfies the goal. Then shrink from the left as long as it still does, recording each size on the way.

Reach for it when you want the smallest run that reaches a sum or covers a set of items.Go deeper: Sliding windows: reuse the work you just did

Monotonic deque

O(n)

Keep indexes in a deque with their values in decreasing order. Anything smaller than a newcomer can never be the maximum again, so drop it.

Reach for it when you need the maximum or minimum of every window, not just a sum.

Two heaps

O(n log k)

Hold the smaller half of the window in a max-heap and the larger half in a min-heap. The middle sits on top of one of them.

Reach for it when you need the median of each window as it slides.Go deeper: How a heap always knows the smallest

The one template to memorise

Learn this one by heart: grow on the right, shrink on the left while the window is broken. Change the rule and the same loop solves most of the list below.

solution.py
def longest_without_repeats(s):
count = {}
left = best = 0
for right, ch in enumerate(s):
count[ch] = count.get(ch, 0) + 1
while count[ch] > 1: # broken: shrink from the left
count[s[left]] -= 1
left += 1
best = max(best, right - left + 1)
return best

Problems, easy to hard

Do them in order the first time
#ProblemPatternDifficultySolution
1Maximum Average Subarray IFixed windowEasyRead
2Contains Duplicate IIFixed windowEasyRead
3Longest Substring Without Repeating CharactersLongest valid windowMediumRead
4Permutation in StringFixed windowMediumRead
5Find All Anagrams in a StringFixed windowMediumRead
6Max Consecutive Ones IIILongest valid windowMediumRead
7Fruit Into BasketsLongest valid windowMediumRead
8Longest Repeating Character ReplacementLongest valid windowMediumRead
9Minimum Size Subarray SumShortest valid windowMediumRead
10Minimum Window SubstringShortest valid windowHardRead
11Sliding Window MaximumMonotonic dequeHardRead
12Sliding Window MedianTwo heapsHardRead
Next upBinary searchPrefix sums