Sliding Window: when to spot it, explain it, and practice it
Sliding window is not just a template. It is a way to turn brute-force range enumeration into a controlled expand-and-shrink process, which is why it dominates substring and subarray interview rounds.
Pattern coverage
50+
Best first move
Decide whether the window is fixed-size or variable-size before coding.
Common failure point
Shrinking on the wrong condition and discarding valid windows too early.
When this pattern should come to mind
Checklist before you code
The solving flow that works well in interviews
Confirm that the answer can be derived from a moving contiguous interval.
Choose the window state: sum, distinct count, frequency, or matched characters.
Expand the right pointer and update the state immediately.
Shrink from the left only while the window is invalid or oversized.
Update the answer with the valid window that remains.
Common variants
Fixed-size window
Best when the window length is given directly, such as all substrings of length k.
Variable-size window
Best when the condition depends on counts, sums, or coverage and the left boundary moves only when invalid.
Window + frequency map
Useful when repeated characters or repeated values define validity.
Template preview
# Variable size sliding window
left = 0
window = {}
answer = 0
for right, value in enumerate(nums):
add(window, value)
while not is_valid(window):
remove(window, nums[left])
left += 1
answer = update(answer, left, right, window)
# Fixed size sliding window
window = initialize(nums[:k])
answer = evaluate(window)
for right in range(k, len(nums)):
remove(window, nums[right - k])
add(window, nums[right])
answer = update(answer, right - k + 1, right, window)
Classic problems with useful framing
#3
Longest Substring Without Repeating Characters
The cleanest entry point for map-based variable windows.
Find the length of the longest substring with no repeated characters.
#76
Minimum Window Substring
Teaches how coverage conditions and shrink logic interact.
Find the smallest substring that contains all characters of t.
#567
Permutation in String
A classic fixed-size window with exact frequency matching.
Check whether one string contains a permutation of another.
#209
Minimum Size Subarray Sum
A clean sum-based window that builds confidence quickly.
Find the shortest subarray whose sum is at least target.
A more useful problem ladder for practice
This is not a random list. It is ordered to help candidates build recognition first, add key variants next, and then increase pressure with harder cases.
Foundation
#3 Longest Substring Without Repeating Characters
Find the length of the longest substring with no repeated characters.
Once a character repeats, moving the left boundary forward is the only way to restore validity; no other operation can help.
Foundation
#209 Minimum Size Subarray Sum
Find the shortest subarray whose sum is at least target.
Positivity makes the window sum monotonic with pointer movement, which is exactly why sliding window beats prefix-sum search here.
Variant depth
#424 Longest Repeating Character Replacement
Find the longest substring that can become all one character after at most k replacements.
The number of replacements needed equals window size minus the count of the most frequent character inside the window.
Variant depth
#567 Permutation in String
Check whether one string contains a permutation of another.
Any valid window must have the same length as s1, so the problem reduces to whether any length-|s1| window matches the needed character counts.
Pressure test
#76 Minimum Window Substring
Find the smallest substring that contains all characters of t.
The key state is not distinct count alone but how many required character frequencies have been fully satisfied.
High-frequency mistakes
Shrinking on the wrong condition and discarding valid windows too early.
Updating the answer before the window becomes valid.
Forgetting to remove state when the left pointer moves.
Mixing fixed-size and variable-size logic in the same loop.
Recommended practice path
Start with fixed-size frequency problems such as permutation matching.
Move to basic variable windows like minimum size subarray sum.
Then solve coverage windows such as minimum window substring.
Finally revisit harder hybrids that combine window logic with maps and counts.