patternDSA/Sliding Window
Core Pattern → Pattern 04

Sliding Window

Transform O(n²) subarray brute force into O(n) elegance. One pointer expands, one pointer shrinks — the window does the work.

Frequency:★★★★★
·~45 min·Intermediate·14 problems
The Core Shock

Max subarray sum — n² inner loops vs one pass

Brute Force
n × k operations
for n=10k, k=100: 1,000,000 additions
O(n·k)
Sliding Window
n + k operations
for n=10k, k=100: 10,100 operations
O(n)

The insight: two adjacent windows of size k share k−1 elements. Instead of re-summing from scratch, subtract the element leaving the left edge and add the element entering the right edge. O(1) per slide.

The Two Window Types

Every sliding window problem uses one of these. Identify which before coding.

Fixed-Size Window

Window size k is given. Slide one step at a time: add right element, remove left element. Window size never changes.

Test: Ask: 'Is k (window size) given in the problem?' If yes → fixed window.
Max sum of k elementsMax average subarray of length kFind all anagramsPermutation in string
Variable-Size Window

Window expands and shrinks based on a condition. Expand right while valid, shrink left when violated. Size changes dynamically.

Test: Ask: 'Do I find the longest/shortest subarray satisfying a condition?' If yes → variable window.
Longest substring no repeatsMin subarray sum ≥ targetLongest with k distinct charsMin window substring

The 3-Step Sliding Window Framework

The thinking process every sliding window expert uses. Internalize it.

01
Initialize the Window

Set up left = 0, right = 0, and any tracking data structures (sum, set, hashmap, frequency counter). For fixed windows, build the initial window of size k first.

left = 0; window_sum = sum(nums[:k]); max_sum = window_sum
What do you need to track inside the window? Just a sum? A set? A frequency map? Choose before writing a single loop.
02
Expand: Move Right Pointer

Advance the right pointer one step at a time. Add the new element to your window state. For fixed windows this happens every iteration. For variable, always expand first.

window_sum += nums[right]  # or char_set.add(s[right])
The right pointer always moves forward. Never move right backward. Expanding is always O(1) — just add the new element to your state.
03
Shrink: Move Left Pointer When Needed

For fixed windows: always remove nums[right - k]. For variable windows: shrink left while the window condition is violated. Update window state as you remove.

while condition_violated:
    remove s[left]; left += 1
The shrink condition is the heart of variable windows. State it precisely: 'shrink while duplicate exists' or 'shrink while sum > target'.

Pattern Recognition

30-second diagnosis: is this sliding window?

Use Sliding Window When
"Maximum/minimum subarray of size k"
"Longest/shortest subarray with condition"
Input is a string or contiguous array
"Find all occurrences of permutation"
Brute force is O(n²) nested loops
"At most k distinct / unique characters"
"Minimum window containing all of X"
Do NOT Use Sliding Window When
Non-contiguous elements (use Two Pointers)
Sorted array without window structure
Need to track global min/max in window (use monotonic deque)
Multiple arrays (often Two Pointers)
Non-sequential access required
DP recurrence needed (not a simple window)