Skip to main content
Back to the DSA atlas
Linear structures Technique Intermediate

Monotonic Queue

Maintain sliding-window candidates in sorted order inside a deque so each window maximum or minimum is available in O(1).

monotonic-queuedequesliding-windowarray

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

front

idx 1

3

idx 2

-1

back

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 O(nk)O(nk). 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

OperationTime
Whole passO(n)O(n)

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

ProblemDifficultyKey idea
Sliding Window MaximumšŸ”“ HardCanonical monotonic-queue problem
Shortest Subarray with Sum at Least KšŸ”“ HardPrefix sums plus monotonic deque
Jump Game VI🟔 MediumDP 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.

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.

More from Linear structures

Stay in the same family when you want parallel variations of the same mental model.

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.