What is a sliding window?
A sliding window is a range [left, right] over an array or string.
You slide it forward one step at a time, and update your answer as you go —
instead of recalculating the whole stretch from scratch every time.
- Window
- The contiguous stretch you're looking at right now
- Fixed
- Width stays
k. Both edges move together. - Variable
- It grows and shrinks when a condition changes
Why slide at all?
Picture a street of houses. You're in a car, and your windshield only shows three houses at a time. Drive forward one house and most of what you see is still there — house 2 and 3 didn't leave. Only the edges changed: one house exited on the left, one entered on the right.
That's the whole point. What's inside the frame mostly survives from one step to the next. You only need to react to the edges. You never re-scan the whole street.
Only the edges change. Everything in the middle stays.
What kinds of problems does it solve?
Four common shapes. Same crawl every time — you only change what you do at each step: keep the best, grow then shrink, check and count, or update a running value.
Best stretch
Slide a fixed window across the array and keep whichever stretch scores highest so far. Classic example: the best three-day spend.
Best stretch so far lands at 120.
Smallest stretch that still works
Grow the window until it satisfies the goal, then try shrinking from the left to see how small it can get while still working.
Shrink while A and B stay inside. Best length is 4.
Count qualifying stretches
Slide across. At each position, ask one question: does this window count? If yes, add one to the tally.
Only the all-Y windows of length 3 count.
Rolling value
The window size never changes. It just glides, and you update one running number each step — drop what left, add what entered.
Drop the leaving value, add the entering one. No full recalculation.
Two types
Under the hood there are really only two shapes: a ruler that never changes length, and a rubber band that stretches and pulls back.
Fixed size
k is given up front. Both edges move together, always exactly k apart.
Fill the first window, then for every new right edge: remove the old left, add the new right.
Both edges advance together. Width never changes.
function fixedWindow(arr: number[], k: number): number {
// seed the first window: combine arr[0..k-1]
for (let right = k; right < arr.length; right++) {
const left = right - k;
// remove arr[left], add arr[right]
// update best / result
}
return result;
}
Variable size
You don't know the width in advance. right always moves forward.
left only moves when the window breaks a rule — then you shrink until it's valid again.
Right always advances. Left only moves when it has to.
function variableWindow(arr: number[]): number {
let left = 0;
for (let right = 0; right < arr.length; right++) {
// add arr[right] into window state
while (/* window needs shrinking */) {
// remove arr[left] from window state
left++;
}
// update result using [left..right]
}
return result;
}
Where it works — and where it breaks
Sliding window leans on a quiet assumption: once a window goes bad, growing it further won't make it good again. That holds for all-positive sums. It falls apart the moment negatives (or anything that undoes progress) show up.
Works with all-positive values
Once the sum crosses the target, adding more only pushes it further past. Shrinking from the left is the only way back — you never need to un-shrink.
Breaks once negatives appear
Growing can repair a bad window. Shrink-left assumes “once bad, only shrinking helps.” With a negative in the mix, that assumption throws away a valid window you never got to see.