CMD Guide
HomeDSAAdvanced Patterns

Introduction to Monotonic Queue Pattern

Mechanism

A monotonic queue is a deque that, on every insertion, evicts elements from its back that can never be useful again — because the new element is both more recent and at least as extreme (larger for a max-tracking queue, smaller for a min-tracking queue) — so the deque stays sorted for free, and its front is always the answer for the current window, retrievable in O(1).

Recognize the pattern

Brute force vs optimal

Brute force: for each of the n − k + 1 windows, scan all k elements for the max. O(n·k) time, O(1) space. For n = 10^5, k = 10^4 that is ~10^9 comparisons — too slow.

Heap: push (value, index) pairs into a max-heap, lazily discard stale (out-of-window) tops. O(n log n) time, O(n) space — correct but pays an unneeded log factor.

Monotonic deque (optimal): keep indices in decreasing value order; drop the front once it exits the window. O(n) time, O(k) space.

Complexity, derived from first principles

Each of the n elements is pushed onto the deque's back exactly once (n pushes). Once pushed, it is removed at most once — either popped from the back when a later, more extreme element evicts it, or popped from the front when the window slides past it, but never both: the first removal takes it out of the deque permanently. So total removals ≤ n, and total deque operations ≤ 2n = O(n). Amortized per element: O(1). Summed over the array: O(n) time. The deque never holds more than k indices at once (stale ones are evicted as the window moves), so space is O(k), plus O(n − k + 1) for the output array if one is required.

Worked example — sliding window maximum

nums = [1, 3, −1, −3, 5, 3, 6, 7], k = 3. Deque stores indices, kept so that nums[deque] is strictly decreasing.

inums[i]pop from back (value ≤ nums[i])pop from front (out of window)deque (indices) afteroutput
01[0]
13pop 0 (1≤3)[1]
2−1nonenone[1,2]nums[1]=3
3−3nonenone[1,2,3]nums[1]=3
45pop 3,2,1[4]nums[4]=5
53nonenone[4,5]nums[4]=5
66pop 5,4[6]nums[6]=6
77pop 6[7]nums[7]=7

Result: [3, 3, 5, 5, 6, 7].

import java.util.*;

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        int[] result = new int[n - k + 1];
        Deque<Integer> dq = new ArrayDeque<>(); // indices, nums[dq] strictly decreasing
        for (int i = 0; i < n; i++) {
            while (!dq.isEmpty() && nums[dq.peekLast()] <= nums[i]) {
                dq.pollLast();
            }
            dq.addLast(i);
            if (dq.peekFirst() <= i - k) {
                dq.pollFirst();
            }
            if (i >= k - 1) {
                result[i - k + 1] = nums[dq.peekFirst()];
            }
        }
        return result;
    }
}

Pitfalls

When to use / when not — trade-offs

Use when you need running max/min over a sliding window (fixed or variable) and can afford O(k) auxiliary space; it is the only O(n) option.

Avoid / alternatives:

Takeaways

Recall: Why is the front of a monotonic decreasing deque guaranteed to be the maximum of the current window, and why is it safe to permanently discard a smaller element that appears before a larger one?


Adapted and expanded from LeetCode / classic sliding-window-maximum treatments (e.g., LeetCode 239) and standard competitive programming references on monotonic deques.

🤖 Don't fully get this? Learn it with Claude

Stuck on Introduction to Monotonic Queue Pattern? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Introduction to Monotonic Queue Pattern** (DSA) and want to truly understand it. Explain Introduction to Monotonic Queue Pattern from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Introduction to Monotonic Queue Pattern** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Introduction to Monotonic Queue Pattern** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Introduction to Monotonic Queue Pattern** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes