AdvancedAbout 25 min

    Monotonic Queue (Sliding Window Maximum)

    Read the full article on Substack

    7 min read.

    Deque-based technique to track max/min in a sliding window in O(n). Essential for Sliding Window Maximum and optimization problems.

    Think of it this way: Like VIP bouncers at a club. As new people arrive, anyone less important gets kicked out from the back. The front always has the most important person currently in the window.

    awindow1031-12-3354356677i
    deque1[0] front
    maxima0

    Push index 0 (1) at the back.

    1/22

    Check your understanding

    Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.

    Which end does it leave from?

    Sort

    For the sliding-window maximum deque, sort each reason an index is removed.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    Popped from the back

    Drop items here

    Popped from the front

    Drop items here
    5 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    In the Sliding Window Maximum problem, how does a Monotonic Queue differ structurally from a Monotonic Stack?

    Done with this lesson?

    Sign in free to keep your progress, streak and quiz scores.

    Keep going