HardArrayQueueSliding WindowHeapMonotonic QueueUpdated Sep 2026

239. Sliding Window Maximum

Asked at Amazon, Microsoft, Walmart, NVIDIA and 2 more

Difficulty

Hard

Time

O(n)

Space

O(k)

Pattern

Array

Companies

6

Problem

You get an integer array nums and a window size k. Imagine a window of exactly k positions that starts at the left end of the array and slides one step to the right until it reaches the right end. At every position, look at the k numbers inside the window and take the largest one.

Return an array that holds the maximum of each window, in the order the windows appear. The answer has nums.length - k + 1 entries, one per window position.

A window may contain negative numbers, so do not assume the maximum is positive. If k equals the array length there is only one window, and the answer is a single number.

One max per window position

numsmax 313-1-3536701234567First window [1,3,-1] — the largest value is 3.numsmax 513-1-3536701234567After two slides the window is [-1,-3,5] and the largest value is5.
The window of size 3 slides right one step at a time, and we record the largest value inside it each time.

Constraints

  • ●1 <= nums.length <= 10^5
  • ●-10^4 <= nums[i] <= 10^4
  • ●1 <= k <= nums.length

Examples

Example 1
Inputnums = [1,3,-1,-3,5,3,6,7], k = 3
Output[3,3,5,5,6,7]
WhyThe windows are [1,3,-1] → 3, [3,-1,-3] → 3, [-1,-3,5] → 5, [-3,5,3] → 5, [5,3,6] → 6, and [3,6,7] → 7. Reading those six maxima in order gives the output.
Example 2
Inputnums = [1], k = 1
Output[1]
WhyThere is only one window and it holds the single value 1, so the answer is [1].

Asked At

6 companies

How to Think About It

1

Brute force: for each window, scan k elements to find the max. O(nk) total. Too slow for large inputs.

2

Better: use a max-heap of size k. For each window, push the new element and pop the old one. O(n log k). Still not optimal.

3

Optimal: monotonic deque. Maintain a deque of indices where values are in decreasing order. The front of the deque is always the current window max.

4

Why the deque works: when a new element arrives, pop all smaller elements from the back. They can never be the max while the new element is in the window. This keeps the deque monotonic decreasing.

5

Edge cases: k=1 (return the array itself), k=n (return global max), all same elements.

Optimal Approach

Use a deque storing indices (not values) in decreasing value order.

For each element:

  1. Remove indices from the back while the back value <= current value (maintains decreasing order).
  2. Push current index to the back.
  3. Remove the front if it is out of the window (index <= i - k).
  4. If i >= k - 1, record deque[0] as the window max.

The deque front always holds the index of the current window max. Elements are removed from the back when a larger element arrives (they can never be the max again). Elements are removed from the front when they slide out of the window.

Time: O(n) — each element is pushed and popped at most once. Space: O(k) for the deque.

Monotonic deque of indices

deque (front on top)1(3)2(-1)frontbackAfter i=2 the deque holds indices 1 and 2; the frontindex 1 has value 3, the window max.deque (front on top)4(5)frontbackAt i=4 the value 5 pops every smaller value from theback, so the deque becomes just index 4.pop back while value <= new valuepush new indexdrop front if index <= i - kif i >= k-1, output nums[front]These four steps run once per element and keep thedeque correct.
The deque keeps indices whose values only decrease, so its front is always the current window max.
ComplexityTime O(n)Space O(k)Each index enters and leaves the deque at most once, and the deque never holds more than k indices.

What Trips People Up in Real Interviews

1

Using a max-heap instead of a monotonic deque. A heap gives O(n log k), but the deque approach is O(n) because each element is pushed and popped at most once.

2

Storing values in the deque instead of indices. You need indices to check if the front element is still within the sliding window (index <= i - k).

3

Forgetting to remove out-of-window elements from the front. The front index might be older than i - k, meaning it has slid out of the current window.

4

Not popping smaller elements from the back. If you keep all elements, the deque is not monotonic and you can't guarantee the front is the max.

5

Returning the deque front value directly without checking if it is in the window. Always check dq[0] <= i - k before recording the max.

Solution Code

O(n) time
from collections import deque

def maxSlidingWindow(nums, k):
    dq = deque()
    result = []
    for i, num in enumerate(nums):
        while dq and nums[dq[-1]] <= num:
            dq.pop()
        dq.append(i)
        if dq[0] <= i - k:
            dq.popleft()
        if i >= k - 1:
            result.append(nums[dq[0]])
    return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Sliding Window Maximum problem?

Given an array and a window size k, return the maximum value in each sliding window of size k. The `monotonic deque` approach achieves `O(n)` time, far better than the naive `O(nk)` or `O(n log k)` heap approach.

How do you solve Sliding Window Maximum?

The optimal approach runs in O(n) time and O(k) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Sliding Window Maximum?

Sliding Window Maximum is asked at Amazon, Microsoft, Walmart, NVIDIA, DE Shaw, JPMorgan. It is a hard difficulty problem.

What are common mistakes on Sliding Window Maximum?
  • Using a `max-heap` instead of a `monotonic deque`. A heap gives `O(n log k)`, but the `deque` approach is `O(n)` because each element is pushed and popped at most once.
  • Storing values in the `deque` instead of indices. You need indices to check if the front element is still within the sliding window (index <= i - k).
  • Forgetting to remove out-of-window elements from the front. The front index might be older than i - k, meaning it has slid out of the current window.
  • Not popping smaller elements from the back. If you keep all elements, the `deque` is not monotonic and you can't guarantee the front is the max.
  • Returning the `deque` front value directly without checking if it is in the window. Always check `dq[0] <= i - k` before recording the max.