Monotonic Queue
Maintain sliding-window candidates in sorted order inside a deque so each window maximum or minimum is available in O(1).
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
Index 2
Current sliding window
idx 0
1
idx 1
3
idx 2
-1
idx 3
-3
idx 4
5
idx 5
3
idx 6
6
idx 7
7
Deque of candidates
idx 1
3
idx 2
-1
Sliding-window maximum
Window [0, 2] keeps candidates in decreasing order, so the front is the max.
Current max: 3
Family
Linear structures
State that flows from one end to the other: lists, deques, stacks, and windows.
Builds on
3 topics
Read these first if you want the surrounding context.
Unlocks
1 next topic
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
1
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
You need an answer for every fixed-size sliding window, such as:
- maximum in every window of size
k - minimum in every window of size
k
A naive solution rescans each window, costing . That is far too slow when the array is long.
Core idea
Keep a deque of candidate indices.
For a maximum queue:
- remove indices that fell out of the window from the front
- remove indices from the back while their values are smaller than the new value
- append the new index
- the front now holds the maximum for the current window
The deque stays in decreasing value order.
Why it works
If a new value is greater than an older value still sitting at the back, that older value can never become the answer for any future window that also contains the new value.
So you delete it permanently.
That one dominance rule is what makes the whole technique linear.
Algorithm sketch
for right in 0..n-1:
remove expired indices from front
while deque not empty and a[deque.back] <= a[right]:
pop_back()
push_back(right)
if right >= k - 1:
answer.push(a[deque.front])
Complexity
| Operation | Time |
|---|---|
| Whole pass |
Each index enters the deque once and leaves once.
Why it matters
Monotonic queues are one of the best examples of a technique that looks niche but appears everywhere once you know to see it:
- sliding-window maximum/minimum
- DP transitions with bounded ranges
- online candidate maintenance where older dominated states can be discarded
It is the queue-side sibling of a monotonic stack.
Key takeaways
- A monotonic queue is not just a queue; it is a queue plus a dominance invariant
- The deque stores candidates, not every value blindly
- Front gives the current answer, back is where dominated candidates get removed
- This is an important non-rare technique because sliding windows are everywhere
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Sliding Window Maximum | š“ Hard | Canonical monotonic-queue problem |
| Shortest Subarray with Sum at Least K | š“ Hard | Prefix sums plus monotonic deque |
| Jump Game VI | š” Medium | DP optimization with a deque |
Relation to other topics
- Queue gives the FIFO processing model
- Deque provides the two-ended operations monotonic queues rely on
- Monotonic stack uses the same dominance idea in a one-directional setting
Build on these first
These topics supply the mental model or underlying structure that this page assumes.
Deque (Double-Ended Queue)
Push and pop from both ends in O(1). A deque can behave like a queue, a stack, or a monotonic window depending on how you use it.
Queue
Process work in first-in, first-out order. Queues are the missing linear-structure foundation behind BFS, buffering, and fair scheduling.
Two Pointers & Sliding Window
Shrink O(n²) brute force to O(n) by maintaining a window or two converging pointers. The go-to technique for subarray and subsequence problems.
What this enables
Once the current idea feels natural, these are the most useful next jumps.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
Prefix Sum
Precompute cumulative totals so any range sum becomes one subtraction. Prefix sums are the first serious range-query idea everyone should know.
Stack & Monotonic Stack
LIFO data structure for expression evaluation, backtracking state, and the powerful monotonic stack pattern for next-greater-element problems.
Dynamic Programming
Break a problem into overlapping subproblems, solve each once, reuse the results. DP turns exponential brute force into polynomial time.
More from Linear structures
Stay in the same family when you want parallel variations of the same mental model.
Deque (Double-Ended Queue)
Push and pop from both ends in O(1). A deque can behave like a queue, a stack, or a monotonic window depending on how you use it.
Linked List Operations
Reverse, detect cycles, merge sorted lists. Linked lists test your pointer manipulation skills ā every operation is about rewiring next pointers.
Queue
Process work in first-in, first-out order. Queues are the missing linear-structure foundation behind BFS, buffering, and fair scheduling.
Stack & Monotonic Stack
LIFO data structure for expression evaluation, backtracking state, and the powerful monotonic stack pattern for next-greater-element problems.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Linear toolkit
Learn how sequence-shaped data unlocks queues, stacks, windows, and fast pointer manipulations.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.