04 Try it yourself Interactive

Wrap the pointers, then open the other end

Press Next one step at a time. In the Circular queue tab the middle row is the array of 5 slots: emerald cells are owned by the queue, the amber cell was just written, the rose cell was just read out. The bottom row is where F and R are pointing — watch them jump from slot 4 to slot 0.

The Window maximum tab runs whichever list is loaded through a deque instead. The emerald cells in the deque row are the indices still in play, and the indigo one is its front — which is the window maximum on every row that reports one, but only after any index that has fallen out of the window has been dropped, so watch it be wrong for exactly one step before that drop happens.

Sliding window maximum, in one rule: keep a deque of indices whose values decrease from front to back. Before pushing i, pop every back index whose value is at most a[i] — it is older and smaller, so it can never win again. Then drop the front if it has fallen out of the window, and the front is the maximum.
Circular queue · capacity 5
every enqueue and dequeue is O(1)
5–9 whole numbers (0–99), commas or spaces. The ring is always capacity 5.
capacity 5 · slot 4 wraps back to slot 0
Press Next to begin. Each press moves one step.


      
Front
Rear
Size0
Refused0
step 0 / 0