Interview patterns
Sliding Window Pattern Explained for Coding Interviews
Updated Nov 27, 2026 · 3 min read
Keep a window over a run of elements that sit next to each other. Grow it by moving the right edge one step at a time; when the window breaks its rule, move the left edge forward until the rule holds again, then update the best answer. Every element enters once and leaves once, so it's O(n) instead of O(n²).
Key points
- ●Clue words: substring, subarray, consecutive, in a row, plus a rule the piece must follow.
- ●Grow the right edge; when the rule breaks, shrink from the left.
- ●Each element goes in once and out once: O(n), not O(n²).
- ●Only for contiguous pieces. Subsequences that skip elements need something else.

The riddle: 36 substrings, but you only need 8 looks
LeetCode 3 asks for the longest substring with no repeated letters. Take "abcabcbb": 8 letters.
Checking every substring means 36 of them, and each one has to be scanned for repeats. A sliding window looks at each of the 8 letters once.
How to spot it
The clue is in the wording. You want one piece, in a row: a substring, subarray, something consecutive. And the piece has to follow a rule:
- No repeats: fix the window when a character shows up again.
- At most k changes: fix it when the number of changes goes over k.
- Sum at least a target: use the sum to decide when the window is valid.
- Fixed size k: keep the window at size k as the right edge moves.
If the elements can be skipped, it's a subsequence, and a window doesn't apply.
The idea: grow right, shrink left
A window has three moving parts, the same three the visual sheet above shows:
- Right edge: moves forward one step every time, growing the window.
- Left edge: moves forward only when the rule breaks, dropping elements until it holds again.
- Rule: must hold before you record the best answer.
Neither edge ever moves backward. That's the whole trick. The brute force throws away everything it learned about one substring and starts over for the next. The window keeps what it knows and only adds one letter on the right or drops one on the left.
Trace it on "abcabcbb"
| Right letter | Repeat? | Window after fixing | Length | Best |
|---|---|---|---|---|
| a | no | a | 1 | 1 |
| b | no | ab | 2 | 2 |
| c | no | abc | 3 | 3 |
| a | yes, drop a | bca | 3 | 3 |
| b | yes, drop b | cab | 3 | 3 |
| c | yes, drop c | abc | 3 | 3 |
| b | yes, drop a and b | cb | 2 | 3 |
| b | yes, drop c and b | b | 1 | 3 |
The answer is 3, the window "abc".
The Python template
def longest(s):
seen = set()
left = best = 0
for right, ch in enumerate(s):
while ch in seen:
seen.remove(s[left])
left += 1
seen.add(ch)
best = max(best, right - left + 1)
return best
A set holds the letters in the window, left is the left edge, and the for loop is the right edge. On a repeat, the while loop drops letters from the left until the repeat is gone. Then the window is valid again and you update the best length.
Complexity
- Sliding window: O(n). The right edge moves n times, the left edge at most n times, so every element goes in once and out once.
- Every substring: O(n²) or worse.
The while loop inside the for loop looks like O(n²), but it isn't. Across the whole run, left only moves forward, n steps at most in total.
Where it shows up, and the trap
The same template, with a different rule, solves LeetCode 3 Longest Substring Without Repeating Characters, 424 Longest Repeating Character Replacement, 567 Permutation in String and 76 Minimum Window Substring.
The trap: updating the best answer before the window is valid again. Fix the window first, then measure it. A second trap is reaching for a window when the problem asks for a subsequence. If elements can be skipped, there is no single window to slide.
Frequently asked questions
When should you use the sliding window technique?
When a problem asks for the best contiguous subarray or substring (longest, shortest, at least, at most) and you can check the window's rule as it grows, like no repeats, at most k changes or a sum of at least a target.
What is the time complexity of sliding window?
O(n). The right edge moves n times and the left edge moves at most n times, so every element enters and leaves the window once. Checking every substring is O(n²) or worse.
Sliding window vs two pointers: what's the difference?
Both use two indexes. In two pointers they usually start at opposite ends and move toward each other. In a sliding window both move forward in the same direction, and everything between them is the current piece.
Does sliding window work for subsequences?
No. A window only works when the elements stay next to one another. A subsequence can skip elements, so it isn't a sliding window problem.