By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(A Complete FAANG-Level Interview Guide)
"This single pattern appears in 1 out of every 3 onsite interviews—master it, and you’ll solve problems in O(n) time with O(k) space, proving you can optimize beyond brute force."
Before tackling Sliding Window Maximum, ensure you understand: 1. Monotonic Deques – A deque (double-ended queue) where elements are kept in a specific order (e.g., decreasing) to enable O(1) max/min queries. 2. Sliding Window Basics – Fixed-size window traversal (e.g., subarrays of size k). 3. Time Complexity Analysis – Why nested loops are O(n²) and how to reduce to O(n).
k
(Repeat this checklist for every sliding window max problem.)
Is the array circular? (May require prefix/suffix max.)
Brute Force Baseline
Write this first to validate correctness.
Optimize with Monotonic Deque
Traverse the array with index i:
i
Edge Cases
k = 1
k = n
Empty array or k = 0 → Return [].
k = 0
[]
Time/Space Complexity
Space: O(k) (deque holds at most k elements).
Test with Examples
[1,3,-1,-3,5,3,6,7]
k=3
[2,2,2,2]
k=2
Negative numbers (e.g., [-7,-8,-1,-2], k=2).
[-7,-8,-1,-2]
Follow-Up Questions
Problem: Given an array nums and integer k, return the max of every sliding window of size k.
nums
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7]
nums = [1,3,-1,-3,5,3,6,7]
k = 3
[3,3,5,5,6,7]
python def maxSlidingWindow(nums, k): return [max(nums[i:i+k]) for i in range(len(nums)-k+1)]
Time: O(nk) (slow for large n).
n
Optimized (Monotonic Deque): ```python from collections import deque
def maxSlidingWindow(nums, k): dq = deque() res = [] for i, num in enumerate(nums): # Remove indices outside the window while dq and dq[0] <= i - k: dq.popleft() # Remove smaller elements from the back while dq and nums[dq[-1]] < num: dq.pop() dq.append(i) # Record max once window is full if i >= k - 1: res.append(nums[dq[0]]) return res ``` - Why this works: - The deque stores indices in decreasing order of values. - The front is always the max of the current window. - Each element is pushed/popped at most once → O(n) time.
Problem: Same as above, but handle duplicates and edge cases.
Input: nums = [7,2,4], k = 2 Output: [7,4]
nums = [7,2,4]
k = 2
[7,4]
from collections import deque def maxSlidingWindow(nums, k): if not nums: return [] dq = deque() res = [] for i, num in enumerate(nums): while dq and dq[0] <= i - k: dq.popleft() while dq and nums[dq[-1]] < num: # Strictly decreasing dq.pop() dq.append(i) if i >= k - 1: res.append(nums[dq[0]]) return res
[2,2,2]
2
k=1
Problem: Given an array nums and integer k, return the max of every subarray of size ≤ k.
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [1,3,3,5,5,6,7] (includes windows of size 1, 2, and 3)
[1,3,3,5,5,6,7]
from collections import deque def maxSlidingWindowVariable(nums, k): dq = deque() res = [] for i, num in enumerate(nums): while dq and dq[0] <= i - k: dq.popleft() while dq and nums[dq[-1]] < num: dq.pop() dq.append(i) res.append(nums[dq[0]]) # Record max for every window ending at i return res
i >= k-1
dq[0] <= i - k
nums[dq[-1]] <= num
<
i >= k - 1
k=0
"Here’s your 60-second cheat sheet for Sliding Window Maximum: 1. Brute force is O(nk) — too slow for large inputs. 2. Monotonic deque is the optimal O(n) solution: - Remove out-of-window indices from the front. - Maintain decreasing order by popping smaller elements from the back. - Record the front of the deque once the window is full. 3. Edge cases: k=1, k=n, duplicates, negatives. 4. Follow-ups: Dynamic window? Circular array? Use two pointers or prefix/suffix max. 5. Avoid traps: Don’t use a heap, store indices (not values), and handle duplicates with < (not <=).
k=n
<=
Walk in, code the deque, and own the interview."
You’re now ready to solve any sliding window max problem in under 15 minutes. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.