A monotonic deque is a double-ended queue whose elements stay in strictly increasing or strictly decreasing order from front to back. The structure is the right tool for sliding window minimum/maximum problems, where a brute force solution would scan every window in O(n · k) time. A monotonic deque brings that down to O(n).
The deque is the sliding-window cousin of the monotonic stack. The monotonic stack solves next greater / next smaller on a static array. The monotonic deque solves the same kind of comparison query, but inside a window that moves across the array.
This chapter covers:
A monotonic deque is a double-ended queue (deque) whose elements maintain a monotonic order, either strictly decreasing or strictly increasing, from front to back. Elements can be added or removed at either end.
In a decreasing monotonic deque, the front holds the largest element, the back holds the smallest, and values from front to back form a non-increasing sequence. Before pushing a new element at the back, all back elements that violate the order are popped.
For example, with 10, 8, 5, 2 in the deque (front to back), the next element pushed must be smaller than 2, or it pops elements off the back until the order holds.
In an increasing monotonic deque, the front holds the smallest element, the back holds the largest, and values from front to back form a non-decreasing sequence.
The key difference from a monotonic stack is the front. A monotonic stack only ever removes from the top (back). A monotonic deque also removes from the front, which is what allows it to handle a moving window that drops elements once they fall outside the window's left boundary.
As with a monotonic stack, store indices in the deque rather than raw values. The index lets you read both the value (via nums[i]) and check whether the index has fallen outside the window.
A monotonic deque is the right structure when both of the following are true:
The most common trigger is the phrase "sliding window" together with "maximum" or "minimum." Common examples:
k, report the max for each window.k DP values.If the problem only needs "the maximum so far" with no eviction, a monotonic stack or a running max variable is enough. The deque pays for the extra front-side bookkeeping; only spend it when eviction is required.
The standard problem is: given an array nums and a window size k, return the maximum value inside every window of size k as it slides from left to right.
Consider:
With k = 3, the windows and their maxima are:
The expected output is [3, 3, 5, 5, 6, 7].
A brute force solution scans each window, taking O(n · k) total.
This runs in O(n · k) time, which is too slow when both n and k are large. For n = 10⁵ and k = 10⁴, this is 10⁹ operations, well beyond the typical 1-second budget.
The monotonic deque solves the same problem in O(n) total time.
The deque holds candidate indices for the current window's maximum, in decreasing order of value from front to back. Two rules maintain the invariant:
i at the back: pop all back indices whose value is less than or equal to nums[i]. They can never be the maximum once a larger element exists to their right within the same window.front_index <= i - k), pop it from the front. The front always holds the largest value currently in the window.Once index i reaches position k - 1 (the right edge of the first complete window), the front of the deque holds the window's maximum.
A note on the JavaScript and TypeScript versions: Array.prototype.shift runs in O(n) on most engines, which technically breaks the O(n) amortized bound. For correctness in a tight benchmark, either use a manual head index that advances without shifting, or import a deque implementation. For interview purposes the shift version is widely accepted because the front pop is triggered at most once per outer step.
Trace the algorithm on nums = [1, 3, -1, -3, 5, 3, 6, 7] with k = 3. The deque stores indices; values are shown in brackets for clarity.
The result array is [3, 3, 5, 5, 6, 7], matching the brute-force output.
The two key moments to study are i = 4 and i = 6.
At i = 4, the front index 1 falls out of the window (the window is now [2..4], so anything with index <= 1 is gone). The front pop reflects this eviction. Then the new value 5 is larger than -3, -1, and 3, so all of them are popped from the back. Only 5 remains, and it is correctly reported as the window maximum.
At i = 6, no eviction is needed (front index 4 is still inside window [4..6]). But the new value 6 is larger than 3 and 5, so both pop from the back. Again, the new value alone dominates the window.
The inner while loop popping from the back, plus the front pop, suggests O(n · k) at first glance, but the total work is O(n). The argument is a counting one.
Each index is pushed onto the deque exactly once, when the outer loop reaches it. Each index is popped at most once, either from the back when a larger element arrives, or from the front when the window moves past it. Across the entire run of the outer loop, the total number of push operations is n, and the total number of pop operations is at most n.
The inner while loop only runs when it can pop something. Its total cost across all outer iterations sums to at most n, not n per outer iteration. The combined work is n pushes, at most n pops, and n outer iterations, which is O(n) total. This is the same amortized counting argument used for monotonic stacks.
Space is O(k) in the worst case (a strictly decreasing array fills the deque to size k before any pops occur).
Reversing the comparison gives the sliding window minimum. The deque stores indices in increasing value order; the front is the window's minimum.
The only change is the inner pop condition: pop from the back while the back's value is greater than or equal to the current value.
The pattern is identical. A single comparison flip switches between min and max.
Some problems involve a window whose right edge advances by one each step, but whose left edge is determined by a condition rather than a fixed offset. Shortest Subarray with Sum at Least K (LC 862) is the classic example: find the shortest subarray with sum at least K, allowing negative values.
The deque holds indices of a prefix-sum array, in increasing order of prefix value. As the right edge i advances:
i minus the prefix at the front index is at least K, the subarray from front+1 to i is a valid answer. Pop from the front and record the length, because moving the left edge further can only make the answer longer or invalid.The shape of the algorithm is the same as fixed-window: maintain a monotonic deque, evict from the front when a condition is met, and pop from the back to preserve the invariant. The only difference is the eviction trigger.
A monotonic stack and a monotonic deque maintain the same kind of ordering invariant. They differ in which ends they remove from.
The two are the same idea at heart. When no eviction from the front is ever needed, a monotonic deque degenerates into a monotonic stack. When the problem adds a window constraint, the stack becomes a deque with one extra rule: pop from the front when the front index leaves the window.
The implementation backing both is usually the same deque type: ArrayDeque in Java, collections.deque in Python, std::deque in C++, VecDeque in Rust, and LinkedList<T> in C#.
The same template solves a recurring family of windowed-max / windowed-min problems:
k, report the max for each window.K. Works with negative values, which is what distinguishes it from the classic two-pointer sliding window.k apart. The DP transition reduces to a sliding window max over the last k DP values.i you can jump to indices i + 1 through i + k, maximize the score. Same windowed-max DP as Constrained Subsequence Sum.dp[i] depends on the max (or min) of dp[i - k .. i - 1] collapses into a sliding window max (or min).A monotonic deque is the right tool when the answer to a sliding-window query is the maximum or minimum of the current window. The structure is the same as a monotonic stack on the back side; the front side adds eviction driven by the window's left boundary.
The two rules to remember are: pop the back to maintain the monotonic invariant when a new element arrives, and pop the front when its index falls outside the window. Indices, not values, go into the deque, so window membership can be checked directly.
The same template handles maximum or minimum (by flipping the comparison), fixed-size or variable-size windows (by changing the eviction trigger), and array problems or DP problems (by feeding the deque prefix sums or DP values instead of raw inputs). Once the template clicks, an entire class of O(n · k) brute-force solutions collapses to O(n).