The sliding window algorithm is what turns "recompute the sum of every possible subarray" into "add one element, remove one element, repeat." Instead of re-scanning a range from scratch every time it shifts by one position, you keep a running result and update it incrementally. That single change is the difference between an O(n²) brute force and an O(n) pass over the same data.
What changed in 2026
- It remains one of the most-tested interview patterns, alongside the two-pointer technique, specifically because so many "find the best contiguous subarray or substring" problems reduce to it.
- Monotonic-deque windows are increasingly taught alongside the basic pattern, since sliding-window-maximum style problems need more than a running sum.
- Streaming and real-time systems use the same idea at scale. Rate limiters and rolling metrics dashboards are sliding windows applied to live data instead of a fixed array.
The core idea: reuse work instead of recomputing it
Every naive check-every-subarray approach repeats work: the overlap between one window and the next is huge, and brute force throws that overlap away. A sliding window keeps a running value (a sum, a count, a set of seen characters) and updates it by exactly the amount that changed.
Fixed-size windows
The window length is constant; only its position moves.
def max_sum_subarray(nums, k):
window_sum = sum(nums[:k])
best = window_sum
for i in range(k, len(nums)):
window_sum += nums[i] - nums[i - k] # add the new, drop the old
best = max(best, window_sum)
return best
Variable-size windows
The window grows while a condition allows it and shrinks once the condition breaks.
def smallest_subarray_at_least(nums, target):
left = total = 0
best = float("inf")
for right, val in enumerate(nums):
total += val
while total >= target:
best = min(best, right - left + 1)
total -= nums[left]
left += 1
return best if best != float("inf") else 0
Sliding window vs the alternatives
| Approach |
Typical complexity |
Idea |
| Brute force (nested loop) |
O(n·k) or O(n²) |
Recompute the sum/state for every window from scratch |
| Sliding window (fixed size) |
O(n) |
Add the entering element, remove the leaving element |
| Sliding window (variable size) |
O(n) amortized |
Expand the right edge, contract the left edge while a condition holds |
| Two-pointer (opposite ends) |
O(n) |
Move pointers toward each other; not always a contiguous range |
Common pitfalls
Forgetting to shrink the window. A window that only grows produces the wrong answer for "smallest" or "at most" style problems.
Off-by-one on the window length. right - left + 1 and right - left are easy to swap; check the formula against a length-1 window.
Assuming any two-pointer problem is a sliding window. Sliding window specifically maintains a contiguous range with an incrementally updated value; two-pointer more broadly covers pointers converging from opposite ends, which is not a window at all.
FAQ
How is this different from the two-pointer technique?
A sliding window is a specific case of two-pointer where both pointers bound a contiguous range and a running value updates incrementally as it moves. See two-pointer technique explained for the broader family, including patterns that are not windows at all.
How do I find the maximum of a sliding window in O(n) total?
Use a monotonic deque that keeps candidate maximums in decreasing order, discarding from the back when a new element makes older ones useless, and from the front once the max falls outside the window.
Does sliding window always give O(n)?
Only when each element enters and leaves the window a constant number of times, and the per-step update is O(1). A per-step update that rescans the window loses the benefit entirely.
When is sliding window the wrong tool?
When the range that matters is not contiguous, or the problem needs non-adjacent elements considered together — that calls for dynamic programming or a different structure.
Where to go next