Skip to the workspace
Pebellslearn by building
Monotonic Structures & Search on the Answer  ›  Lesson

Sliding Window Extrema

Monotonic Deques 28 minA deque kept in sorted order gives every window's maximum or minimum in one pass

dsa / Sliding Window Extrema

Explore each scene at your own pace.

01 · Three moves, one invariant

Three moves, one invariant

Sliding Window Extrema

Definition
A monotonic deque is a double-ended queue kept in sorted order, used to track the maximum or minimum of a sliding window. Weaker items are removed from the back when a new one arrives, and expired items are removed from the front, so each item is handled once.
In plain words
Imagine a line of candidates for the title of best in the last k days. A newcomer sends away every weaker candidate waiting behind them, and anyone who has been there too long leaves from the front. The person at the front is always the best.

In short: a monotonic stack only ever removes from one end, which is fine until old items need to expire out of a sliding window whether or not anything beat them. A deque gives a second exit: new arrivals evict dominated items from the back, and the front is evicted when it slides out of the window. What's left is always sorted, so the front is always the window's max (or min) — and every index enters and leaves once, so the whole scan is O(n) no matter how big the window is.

A deque holding 60, 30, 20 (decreasing front to back): 60 expires off the front when the window slides, and an arriving 50 evicts 30 and 20 from the back because both are weaker and now older

Quarry's live dashboard draws a band under its latency graph: for the last k requests, the fastest and the slowest. Every new request shifts the window by one, so the band has to be recomputed for every request. Rescanning the window each time costs O(k) per request and O(n·k) for a trace. With k = 1000 and a busy engine, that's a thousand comparisons for every request served, just to draw a line.

The previous lesson's stack answered "who is the next bigger one?" because items only ever left from the top. A window needs a second exit: old requests expire off the far end whether or not anything beat them. A structure with two exits is a deque, and a deque kept in sorted order is a monotonic deque.

Three moves, one invariant

You met the sliding-window maximum briefly in the queues lesson. Here is the idea again, stated so it can be reused. Keep a deque of indices whose latencies decrease from front to back. For each new request i:

  1. Evict from the back every index whose latency is less than or equal to the new one. While the new request is in the window, those can never be the maximum, because it is at least as slow and it will stay longer.
  2. Push i onto the back.
  3. Evict from the front if the front index has slid out of the window, which is when it is <= i - k.

After those moves the front is the window's maximum. It's the oldest survivor, and everything behind it is smaller.

python

Watch the deque on that trace. The front, on the left, is always the answer:

watch it move · stack

New requests evict weaker ones from the back, and time evicts old ones from the front. What's left is always sorted, so the front is the window's worst request.

Request 0 enters an empty deque. Deque, 1 item. Head is 40 (#0), tail is 40 (#0).

1 / 10

Each index enters the deque once and leaves at most once, from one end or the other, so the whole pass is O(n) whatever k is.

Scene 1 of 14
Project context and further resources
You're building a piece ofQuarry — the query planner
This piece — window_band(): The fastest and slowest request in every window of the live dashboard, in one pass.
Scenario Quarry's dashboard shades a band under the latency graph showing the fastest and slowest of the last k requests. It moves with every request served, so it has to cost O(1) per request on average, not O(k).
window_band.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
window_band(latencies, k) → list[fastest, slowest] for every full window of k consecutive requests.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.