A Window Over a Bigger List
You have a long list, and you keep asking a question about a small connected stretch of it — the biggest sum of any 3 numbers in a row, the longest run with no repeated letter. There are thousands of such stretches, and you want the best one.
The obvious way: take each stretch, walk it, and work out its answer from scratch. For "biggest sum of K in a row" over N numbers that is N stretches times K work each — O(N×K). And it is wasteful in a way you can feel: when you slide one step to the right, the new stretch shares almost every number with the old one. You just re-added K numbers when only two of them actually changed.
Don't Rebuild — Slide
Keep a running summary, update only the edges
Hold a window over the list — a left edge and a right edge — and keep a small running summary of what is inside it: a sum, a count of letters, whatever the question needs.
When the window moves one step right, you do not recompute. You add the one element that just entered on the right and subtract the one that just left on the left. Two operations per step, no matter how wide the window is. The K numbers in the middle never get touched again.
left and right is always a valid stretch for the question. You grow the right edge to explore; the moment the rule breaks, you shrink the left edge until the promise holds again. Keep it, and the answer is always sitting in front of you — everything else is just bookkeeping.Watch the Waste Disappear
Step through both. The Naive run re-adds every number in the window at each stop; the Sliding Window run only touches the two that changed. Watch the operation counter.
Initial window [0,1,2]: add all 3 once: 1 + 2 + 3 = 6
A Fixed-Width Window
Same window, first shape: it never changes size — always exactly K wide. Grow the right edge by one, then immediately shrink the left edge by one to hold the width. Reach for this when the question fixes the size: the max sum of K in a row, or an anagram of a fixed word.
Fixed Trace Demo: Max Sum Subarray of size K = 3
Variables Monitor
The Loop, Four Moves
A Window That Breathes
Same window, now it changes size. The right edge keeps exploring forward. The moment the stretch breaks the rule — sum over the limit, or a repeated letter — the left edge steps in until the window is valid again. That is the invariant in action, and the answer is the best valid window you ever saw.
Variable Trace Demo: Longest Subarray with Sum ≤ 7
Variables Monitor
The Loop, Four Moves
Performance Dashboard
TIME EFFICIENCY
Each element is processed exactly twice (once entering on the right, once leaving on the left), making it radically faster than checking every combination.
CRITICAL: When it Fails
Sliding Window fails with negative numbers. If subtracting an item makes the sum bigger, the window loses its clear "direction" and doesn't know how to repair itself.
Summary Comparison
| Window Style | Constraint Condition | Pointers Setup | Time / Space Complexity |
|---|---|---|---|
| Fixed Size | Size is strictly K | right expands, left shifts when size matches K | O(N) / O(1) |
| Variable Size | Length adjusts dynamically | right expands, left contracts while invalid | O(N) / O(1) |
🔎 When to reach for it
- 📏A contiguous stretch: the question is about a subarray, substring, or sliding block — never a scattered, non-contiguous pick.
- 📊Longest / shortest / best under a rule: longest substring with K distinct letters, shortest subarray reaching a target sum, best average of size K.
- ⚡The naive answer re-scans overlapping stretches: O(N×K) or O(N²) work, most of it repeated — exactly the waste a sliding window removes.
Ready to Practice with Visual Simulations?
"Add from the right, check the rule, and shrink from the left."