Sliding Window Maximum and the Monotonic Deque
Master the sliding window maximum using a monotonic deque on DSA Tracker. Learn how indices enter and leave once for optimal linear time.
On this page
You stop the nested loops of O(n * k) by storing indices in a double-ended queue that stays strictly decreasing from front to back. When a new element arrives, pop smaller elements from the tail, remove indices outside the current window from the head, and read the maximum instantly at the front.
Why Nested Loops Fail
A brute force scan checks every window of size k separately. If n is 100,000 and k is 40,000, that scan performs 4 billion operations and times out on the judge. The problem is redundant work. When the window slides right by one step, k-1 elements stay identical. Recomputing their maximum from scratch ignores what you already calculated.
The Monotonic Deque Mechanism
A monotonic deque stores indices, not values, in decreasing order of their corresponding array elements. Look at the array [1, 3, -1, -3] with k = 3. At index 1, the value 3 arrives. It is larger than the previous element 1, so 0 pops off the tail of the deque. The deque holds indices, so 1 sits at the front. When index 3 arrives with value -3, it enters the tail because it is smaller than 3 and -1, keeping the structure intact.
Each index enters the deque once and leaves the deque once. The total push and pop operations across the entire array equal 2n, which guarantees O(n) time complexity even if k is large. If you used a balanced binary search tree or a max-heap, insertions would cost O(log k), raising the total time to O(n log k) and failing strict time limits.
A Hand Calculation
Take the array [1, 3, -1, -3, 5, 3] with k = 3.
- Index
0(val = 1): Deque is[0]. Window[1]max is none yet. - Index
1(val = 3):3 > 1, pop0. Deque is[1]. Window[1, 3]max is none. - Index
2(val = -1):-1 < 3. Deque is[1, 2]. Window[1, 3, -1]max is at front index1, which gives3. - Index
3(val = -3): Index1is out of the window bounds[3 - 3 + 1, 3], so pop head. Deque is[2, 3]. Window[3, -1, -3]max is at index2, which gives-1. - Index
4(val = 5):5beats-3and-1, pop both. Deque is[4]. Window[-1, -3, 5]max is at index4, which gives5.
The Rule of Thumb for Deques
Keep these four rules in mind for every sliding window problem that needs extremes:
- Store indices in the deque, not the actual values, so you can check if an element is outside the current window by comparing index bounds.
- Maintain strict monotonic order by popping the tail while the incoming element is greater for maximums, or smaller for minimums.
- Evict from the head whenever the front index is less than
i - k + 1. - Read the answer for the current window directly from the front of the deque before moving the right pointer forward.
Beyond Basic Windows
The same deque logic applies when the window size is not fixed. Consider Shortest Subarray with Sum at Least K, where the window expands and shrinks based on prefix sums. Here, the monotonic queue stores prefix sum indices and pops elements from the head whenever the sum condition is met, turning what looks like an O(n^2) subarray search into a clean O(n) pass.
Practice this exact implementation on DSA Tracker today by writing the maxSlidingWindow function without looking at a reference solution.
Keep going with the step-by-step visualizer and practice problems.
Further reading: Binary search on Wikipedia.
Frequently asked questions
Why do we store indices instead of values in the deque?
Storing indices lets us check if the element at the front of the deque has fallen outside the current window boundary of size k.
What is the time complexity of the monotonic deque approach?
It runs in `O(n)` time because each index is pushed and popped from the deque at most once.
Can this pattern find minimums as well as maximums?
Yes, you just change the comparison condition at the tail of the deque to maintain a monotonically increasing queue instead.
Practice what you just read
Keep reading
How to Validate Insert Delete and Search Binary Search Trees
Master Binary Search Trees by focusing on the inorder traversal property. Learn to validate, insert, delete, and find the Kth smallest element efficiently.
Mastering Matrix Traversal and Search in Interviews
Master matrix interview questions on DSA Tracker by learning index bookkeeping tricks for spiral, rotate, and search 2D grid problems.