Sliding Window Maximum
Given an array nums and a sliding window of size k Return an array of the maximum value in each window position Example: nums = [1,3,-1,-3,5,3,6,7], k = 3 => [3,3,5,5,6,7] Use a deque (double-ended queue) for O(n) time complexity
This problem is a great test of whether you can keep window state consistent as it moves. It's a hard one for a reason: it stresses stateful scanning when the rules pile up. Be disciplined about counts and boundaries; most bugs are off-by-one window moves.
Examples
You write
max_sliding_window(nums: List[int], k: int)
Example 1
- Input
- nums = [1,3,-1,-3,5,3,6,7], k = 3
- returns
- [3,3,5,5,6,7]
Example 2
- Input
- nums = [1], k = 1
- returns
- [1]
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsStack
I reach for it when
- Things must close in the reverse order they opened.
- You need the nearest earlier or later item that is bigger or smaller.
Say this before you type. Say what each item on the stack is waiting for, and what lets you pop it.
All 17 Stack problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.