What a sliding window is
[2, 1, 5, 1, 3, 2] and keep asking: what's the highest total over any 3 consecutive days? (Here it's days 3–5: 5 + 1 + 3 = 9.) The naive way re-adds each 3-day block from scratch — 2+1+5, then 1+5+1, then 5+1+3 — which is O(n·k) and re-adds numbers it just added a moment ago.O(1), no matter how wide the window.This section has a step-by-step animation in the interactive lesson.
Quick check
Sliding a fixed window one step means…
Answer it in the interactive lesson to keep your progress.
How it works, step by step
The steps
- Add up the first
kelements — that's the first window's total. Record it asbest. - Slide the window one step right: add the element entering on the right and subtract the one leaving on the left.
- Update
bestif the new total is larger. - Repeat until the window reaches the end.
Trace it on [2, 1, 5, 1, 3, 2], window size 3:
- first window
2+1+5 = 8→best = 8 - slide:
+1(enters),−2(leaves) →8+1−2 = 7 - slide:
+3,−1→7+3−1 = 9→best = 9 - slide:
+2,−5→9+2−5 = 6
best = 9, in one sweep — each slide was just an add and a subtract. In JavaScript, one way to write those steps:function maxSumWindow(arr, k) {
let sum = 0;
for (let i = 0; i < k; i++) sum += arr[i]; // total of the first window
let best = sum;
for (let i = k; i < arr.length; i++) {
sum += arr[i] - arr[i - k]; // slide: add the entering value, drop the leaving one
best = Math.max(best, sum); // keep the biggest total seen
}
return best;
}
console.log(maxSumWindow([2, 1, 5, 1, 3, 2], 3)); // 9Quick check
The window total is 7 on [2, 1, 5, 1, 3, 2] covering indices 1..3. Predict the total after one slide.
Answer it in the interactive lesson to keep your progress.
Where it breaks (and how not to get burned)
- Forgetting to shrink. In a variable window ("longest stretch with rule X"), you must move the left edge to restore the rule, not just extend the right — otherwise the window is never valid again.
- Recomputing inside the loop. If you find yourself re-summing the window each step, you have lost the whole benefit; keep the running total.
- Off-by-one on width. A window from
lefttorightinclusive hasright - left + 1elements — a classic fencepost slip.
Quick check
A window covering indices left…right inclusive has length…
Answer it in the interactive lesson to keep your progress.
Take it further — longest substring without repeats
right to add characters, and when a repeat sneaks in, move left past the earlier copy so the window is always repeat-free. A Set of the characters currently inside the window (the hash-table tool from two lessons ago) answers "is this a repeat?" in O(1). Track the best length seen.Trace it on abcabcbb
- grow:
a,ab,abc— no repeats, best = 3 righthits the seconda— repeat! shrink:leftsteps past the firsta→ windowbcarighthitsb— repeat again → windowcab, thenabc… the window keeps sliding at length 3- the trailing
bbforces the window down tob— best stays 3
right advances every step, but left only jumps when a repeat appears — and it jumps past the earlier copy, never one-by-one back. Before each step, predict whether the window will grow or shrink.This section has a step-by-step animation in the interactive lesson.
function longestUnique(s) {
const seen = new Map(); // char -> last index seen
let left = 0, best = 0;
for (let right = 0; right < s.length; right++) {
const c = s[right];
if (seen.has(c) && seen.get(c) >= left) left = seen.get(c) + 1; // repeat inside window → jump left past it
seen.set(c, right); // record where we just saw c
best = Math.max(best, right - left + 1); // window width = right - left + 1
}
return best;
}
console.log(longestUnique('abcabcbb')); // 3Quick check
Window is "bca" on abcabcbb and right lands on b — a repeat. Predict where left goes.
Answer it in the interactive lesson to keep your progress.
How fast is it? (complexity)
| Approach | Time | Space | Why |
|---|---|---|---|
| re-sum every block | O(n·k) | O(1) | re-adds overlapping values |
| sliding window (fixed) | O(n) | O(1) | one add + one subtract per slide |
| sliding window (variable) | O(n) | O(k) | a Set/map of what is inside |
O(n) argument.- Keep a running total/state; on each slide, add the entering value and drop the leaving one.
- Fixed windows move both edges together; variable windows grow right and shrink left on a rule.
- One
O(n)pass — never re-sum the whole window.