AlgoScope

Sliding Window Maximum

AlgorithmadvancedQueue Patterns

A monotonic deque of indices keeps the window maximum at the front in amortized O(1).

Decision · step 3 of 23Sliding Window Maximum: Maximum of every 3
active 0-11031-12-3354356677i1 < 3

3 enters. The back of the deque is 1, which is not larger than 3 and will leave the window sooner. It can never be the maximum again, so pop it.

Open in the player →or start at step 3

What you will see

The window slides; the deque drops dominated values from the back and expired ones from the front.

How sliding window maximum works →

Cost

BestO(n)
AverageO(n)
WorstO(n)
SpaceO(k)

How you work with it here

play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next, try operations in any order.

Screen readers: Items announce position from the top or front and value; each step announces the operation and the new top or front.

Reduced motion: Items appear or disappear in place with a crossfade instead of sliding in or out.

Taught by the same lesson

Sliding Window Maximum covers these too, in the same run.