Skip to main content
Monotonic Deque

Sliding Window Maximum

Maintain a decreasing deque of indices. Before adding a new element, remove all smaller elements from the back (they'll never be the max). The front is always the current window's maximum. Remove the front if it's outside the window.

O(n)
·
O(k)

How It Works

Sliding window maximum asks for the largest value in every window of size k. Recomputing each window costs O(nk); a monotonic deque cuts this to O(n). Keep a deque of indices whose values are strictly decreasing from front to back. Before pushing a new index, pop from the back every index whose value is smaller or equal — those elements can never be a future maximum while the newer, larger element is in the window. Pop from the front when its index slides out of range. The front is always the current window's maximum.

Every index enters and leaves the deque at most once, so the whole scan is amortized O(1) per element with O(k) space.

Step-by-Step Visualization

Max in each window of size k=3
1
0
3
1
-1
2
-3
3
5
4
3
5
Window [0..2]
Deque[3, -1]
Max3
1/3

Code

Java
static int[] maxSlidingWindow(int[] nums, int k) {
  Deque<Integer> deque = new ArrayDeque<>();
  List<Integer> result = new ArrayList<>();
  for (int i = 0; i < nums.length; i++) {
    while (!deque.isEmpty() && deque.peekFirst() < i - k + 1) deque.pollFirst();
    while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) deque.pollLast();
    deque.addLast(i);
    if (i >= k - 1) result.add(nums[deque.peekFirst()]);
  }
  return result.stream().mapToInt(Integer::intValue).toArray();
}

Tips & Gotchas

1Maintain a decreasing deque of indices
2Remove from back if current element is larger (they can never be the max)
3Remove from front if index falls outside window

Practice Problems

  • 1Sliding Window Maximum
  • 2Shortest Subarray with Sum at Least K
  • 3Constrained Subsequence Sum
  • 4Jump Game VI

About the Monotonic Deque Pattern

A deque where elements are kept in sorted order. When sliding a window across an array, the front of the deque always holds the current window's max (or min). This gives O(1) per-window extreme lookups.

Key insight

BFS = queue. If you need shortest path in an unweighted graph or level-order traversal, reach for a queue. Monotonic deques solve sliding window extremes in O(n).

Common Queue / Deque Interview Problems

  • Binary Tree Level Order Traversal
  • Sliding Window Maximum
  • Rotting Oranges
  • Shortest Path in Binary Matrix
  • Implement Queue using Stacks

Frequently Asked Questions

Why is a deque required rather than a plain stack or queue?

You need evictions at both ends: stale indices leave the front as the window advances, while dominated smaller values leave the back as bigger elements arrive. A stack only offers one end and a plain queue cannot remove dominated elements, so neither maintains the invariant.

How does this compare with a max-heap approach?

A heap gives O(n log n) with lazy deletion of out-of-window entries, versus the deque's O(n). The heap generalizes more easily (arbitrary removals, weighted variants), but for the standard fixed-size window the deque is both faster and simpler.

Why do dominated elements never matter again?

If a newer element is at least as large, it outlives every older smaller element in all future windows and beats them in value. The smaller element can therefore never be the answer for any window that still contains it, so discarding it immediately is safe.