Coding interview pattern
Track a contiguous subarray or substring without re-scanning it from scratch.
The idea
The sliding window pattern turns a naive nested-loop scan over every subarray (which is quadratic or worse) into a single linear pass. Instead of recomputing a property for each window from scratch, you maintain a window with two boundaries and update an aggregate (a sum, a count, a frequency map) incrementally as the window moves. When the right edge advances you add the new element; when the left edge advances you remove the old one. The work per step is constant, so the whole scan is linear.
There are two flavours. A fixed-size window slides a window of constant length k across the array, which fits problems like maximum sum of any k consecutive elements. A variable-size window grows the right edge to satisfy a condition and shrinks the left edge when the condition breaks, which fits problems like the longest substring without repeating characters or the smallest subarray with a sum at least a target.
The mental model that prevents bugs: the window always represents a valid (or about-to-be-validated) candidate, and you only ever move boundaries forward. Because neither pointer ever moves backward, the amortised cost stays linear even though the inner shrink loop looks nested.
Recognition
Cost
| Operation | Time | Space |
|---|---|---|
| Fixed-size window scan | O(n) | O(1) or O(k) |
| Variable-size window scan | O(n) | O(k) for the frequency map |
| Brute force (what you replace) | O(n^2) or O(n*k) | O(1) |
Worked example
Translatable skeleton
Language-agnostic on purpose. Translate it to your language of choice.
left = 0; for right in range(n): add(s[right]); while invalid(): remove(s[left]); left += 1; best = max(best, right - left + 1)Practice
Work these in order, easy to hard. Each is chosen to drill a different facet of the pattern.
| Problem | Level | Why it fits |
|---|---|---|
| Maximum Average Subarray I | Easy | Canonical fixed-size window. |
| Longest Substring Without Repeating Characters | Medium | Classic variable window with a seen-set. |
| Minimum Size Subarray Sum | Medium | Shrink while the sum stays at least the target. |
| Permutation in String | Medium | Fixed window plus frequency-count matching. |
| Longest Repeating Character Replacement | Medium | Window valid while length minus max-freq is at most k. |
| Minimum Window Substring | Hard | Variable window with a need-count, the hardest of the set. |
Avoid these
In the interview
Recognising the pattern is half the score; saying the right things out loud is the other half. These are the sentences that earn the signal.
Say the word window out loud and state whether it is fixed or variable before you write code; that single sentence tells the interviewer you have recognised the pattern rather than stumbled into it.
Narrate the invariant the window maintains, for example the window always contains no repeated characters, because the invariant is what justifies that the answer is correct at every step.
When you shrink, explain that the left pointer never moves backward, which is the argument for why the whole thing is linear despite the inner loop looking nested.
sliding window leetcode - longest substring pattern - fixed and variable window
An external resource we recommend. AlgoExpert is not affiliated with us and we earn nothing from this link.
Keep going
Walk two indices toward or alongside each other to avoid a nested loop.
Halve the search space each step over any monotonic condition, not just sorted arrays.
Solve overlapping subproblems once, store the results, and build the answer up.
Explore level by level with a queue; the natural fit for shortest paths on unweighted graphs.
See all coding patterns, the coding questions for your role, and the behavioral round.