Sliding Window Maximum seems straightforward: find the max in each window. But 4 common mistakes cause most solutions to fail.
This guide shows you the exact bugs and how to fix them permanently.
TL;DR
The 4 mistakes:
- Using stack instead of deque (can't remove from front)
- Wrong window boundary (
< i - kinstead of< i - k + 1) - Not removing out-of-window elements (forgot
popleft()) - Adding result too early (before window is full)
Quick fixes:
from collections import deque
dq = deque() # Not stack!
# Remove out-of-window
while dq and dq[0] < i - k + 1: # Correct boundary
dq.popleft() # Remove from front
# Add result only when window is full
if i >= k - 1:
result.append(nums[dq[0]])Mistake 1: Using Stack Instead of Deque
The Bug
❌ Wrong:
def maxSlidingWindow(nums, k):
result = []
stack = [] # WRONG: Can't remove from front!
for i in range(len(nums)):
# How to remove out-of-window elements?
# stack.pop() only removes from top!
# ...Why It's Wrong
Sliding window needs to remove from BOTH ends:
- Front: Remove out-of-window elements
- Back: Remove smaller elements
Stack only supports one end!
The Fix
✅ Correct:
from collections import deque
def maxSlidingWindow(nums, k):
result = []
dq = deque() # CORRECT: Can remove from both ends
for i in range(len(nums)):
# Remove from front: O(1)
while dq and dq[0] < i - k + 1:
dq.popleft()
# Remove from back: O(1)
while dq and nums[i] > nums[dq[-1]]:
dq.pop()
dq.append(i)
if i >= k - 1:
result.append(nums[dq[0]])
return resultMistake 2: Wrong Window Boundary
The Bug
❌ Wrong:
# Wrong boundary check
while dq and dq[0] < i - k: # Off by one!
dq.popleft()Why It's Wrong
Window of size k at position i:
- Start:
i - k + 1 - End:
i - Elements outside:
< i - k + 1
Visual:
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
At i=3 (processing -3):
Window: [i-k+1, i] = [1, 3]
Indices: 1, 2, 3
Values: 3, -1, -3
Out of window: index < 1
Correct check: dq[0] < 3 - 3 + 1 = 1
Wrong check: dq[0] < 3 - 3 = 0 (keeps index 0!)The Fix
✅ Correct:
while dq and dq[0] < i - k + 1: # Correct boundary
dq.popleft()Mistake 3: Not Removing Out-of-Window Elements
The Bug
❌ Wrong:
# Forgot to remove out-of-window elements!
for i in range(len(nums)):
# Missing: while dq and dq[0] < i - k + 1: ...
while dq and nums[i] > nums[dq[-1]]:
dq.pop()
dq.append(i)
if i >= k - 1:
result.append(nums[dq[0]])Result: Deque contains elements outside current window, wrong maximum.
The Fix
✅ Correct:
for i in range(len(nums)):
# MUST remove out-of-window first
while dq and dq[0] < i - k + 1:
dq.popleft()
while dq and nums[i] > nums[dq[-1]]:
dq.pop()
dq.append(i)
if i >= k - 1:
result.append(nums[dq[0]])Mistake 4: Adding Result Too Early
The Bug
❌ Wrong:
for i in range(len(nums)):
# ...
dq.append(i)
# WRONG: Adds result before window is full
result.append(nums[dq[0]])Result: First k-1 results are wrong (window not full yet).
The Fix
✅ Correct:
for i in range(len(nums)):
# ...
dq.append(i)
# CORRECT: Only add when window is full
if i >= k - 1:
result.append(nums[dq[0]])Why i >= k - 1?
- First full window: indices 0 to k-1
- At i=k-1, window is full for the first time
Complete Correct Solution
from collections import deque
def maxSlidingWindow(nums, k):
"""
LeetCode #239: Sliding Window Maximum
Time: O(n)
Space: O(k)
"""
if not nums or k == 0:
return []
if k == 1:
return nums
result = []
dq = deque() # Store indices, maintain decreasing order
for i in range(len(nums)):
# 1. Remove out-of-window elements
while dq and dq[0] < i - k + 1:
dq.popleft()
# 2. Remove smaller elements (they'll never be max)
while dq and nums[i] > nums[dq[-1]]:
dq.pop()
# 3. Add current element
dq.append(i)
# 4. Add to result only when window is full
if i >= k - 1:
result.append(nums[dq[0]])
return result
# Test
print(maxSlidingWindow([1,3,-1,-3,5,3,6,7], 3))
# Output: [3, 3, 5, 5, 6, 7]Edge Cases
Edge Case 1: k = 1
nums = [1, 2, 3], k = 1
# Every element is its own window max
# Output: [1, 2, 3]Edge Case 2: k = n
nums = [1, 2, 3], k = 3
# Single window containing all elements
# Output: [3]Edge Case 3: All Same
nums = [3, 3, 3, 3], k = 2
# Output: [3, 3, 3]Debugging Checklist
-
Using deque, not stack?
-
Boundary:
dq[0] < i - k + 1? -
Removing out-of-window before processing?
-
Adding result only when
i >= k - 1? -
Tested edge cases (k=1, k=n, all same)?
Conclusion
The 4 fixes:
- Use deque:
from collections import deque - Correct boundary:
< i - k + 1 - Remove out-of-window:
dq.popleft() - Result timing:
if i >= k - 1
The template:
from collections import deque
dq = deque()
for i in range(len(nums)):
while dq and dq[0] < i - k + 1:
dq.popleft()
while dq and nums[i] > nums[dq[-1]]:
dq.pop()
dq.append(i)
if i >= k - 1:
result.append(nums[dq[0]])Next steps:
Want to Practice LeetCode Smarter?
LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.
Also compatible with Edge, Brave, and Opera
