Monotonic Queue (Sliding Window Maximum)
Deque-based technique to track max/min in a sliding window in O(n). Essential for Sliding Window Maximum and optimization problems.
Monotonic Queue (Sliding Window Maximum)
Deque-based technique to track max/min in a sliding window in O(n). Essential for Sliding Window Maximum and optimization problems.
Sliding Window Maximum: array=[1,3,3,-1,5,5,6,7], k=3. Deque stores indices in decreasing value order. Front = current window max.
Which end does it leave from?
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 herePopped from the front
Drop items hereTest your knowledge
In the Sliding Window Maximum problem, how does a Monotonic Queue differ structurally from a Monotonic Stack?
The short version
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.
In more depth
Deque Magic
Monotonic Queue maintains elements in sorted order within a sliding window.
For Maximum in Window:
Key Insight:
Elements are stored in decreasing order. When the max leaves the window, the next max is already at the front.
Key Gotchas:
When to reach for this
- Max/Min in every sliding window of size k
- DP optimization with fixed-range max lookup
- Subarray problems with window constraints
- Keywords: 'sliding window', 'maximum/minimum in every window'
Interview Questions
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required