Coding interview problems › Sliding Window
🪟 Sliding Window Hard
Sliding Window Maximum
Sliding Window Maximum explained: a monotonic deque of indices keeps the window max at the front, giving every window's maximum in O(n). Tested Python.
Also searched as: maximum of all subarrays of size k · monotonic deque sliding window maximum
Try it live Free
Sliding Window
Slide a fixed window across the array as elements enter and leave. Tracking the maximum needs one more idea from the monotonic stack: drop any waiting value the moment a bigger one arrives.
Also useful: Stacks & the Monotonic Stack
The problem
Given a list of integers and a window size k, slide the window from left to right one step at a time and return the maximum of each window.
The key insight
Keep a deque of indices whose values are in decreasing order. When a new value arrives, pop every smaller value from the back: those can never be a maximum again, because the new value is both larger and will stay in the window longer. The front of the deque is always the current maximum; drop it once it slides out of the window.
Step-by-step approach
- Create an empty deque
dqof indices. - For each index
i: while the back ofdqhas a value<= nums[i], pop it. - Append
i. - If the front index is
<= i - k, it has left the window, so pop it from the front. - Once
i >= k - 1, appendnums[dq[0]]to the answer.
Python solution
from collections import deque
def max_sliding_window(nums, k):
dq, out = deque(), [] # indices, values decreasing
for i, x in enumerate(nums):
while dq and nums[dq[-1]] <= x:
dq.pop() # smaller values can never be max again
dq.append(i)
if dq[0] <= i - k:
dq.popleft() # front slid out of the window
if i >= k - 1:
out.append(nums[dq[0]])
return outComplexity
Every index is appended once and popped at most once, so all deque work adds up to O(n). The deque never holds more than k indices.
Common mistakes
- Storing values instead of indices, which makes it impossible to tell when the front has left the window.
- Popping only strictly smaller values but then mishandling duplicates; popping
<=keeps the newest copy and is simplest. - Starting to emit answers before the first full window, at
i < k - 1.
Frequently asked questions
What is the time and space complexity of Sliding Window Maximum?
The monotonic deque solution is O(n) time and O(k) space. A max-heap solution is O(n log n), and the brute force is O(n·k).
Why can smaller elements be removed from the deque for good?
A smaller element that arrived earlier will leave the window before the new larger one, so while it is in the window the larger one is always there to beat it.
How is this related to the monotonic stack?
It is the same pop-while-smaller rule as Next Greater Element. The deque adds one thing: removing expired indices from the front.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Sliding Window Sliding Window lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.