hardSliding WindowAmazonGoogleLinkedIn
Window Peak Scanner
A moving spotlight of width k scans a row of sensor readings. For every spotlight position, report the brightest reading inside it.
Given an integer array `nums` and window size `k`, return the maximum value in every window of length k as it slides left-to-right.
Choose approach
1
03
1-1
2-3
35
43
56
67
7Mapdeque (idx→val)
empty
1/23
Narrationstep 1 of 23
Monotonic deque of indices whose values are strictly decreasing. The front is always the maximum of the current window.
Variables
k=3
i=0
How to think about it
- A value smaller than a later value can never be a future max — pop it from the back.
- Indices that fall out of the window must be popped from the front.
- Each index is pushed and popped at most once → amortized O(n).
- Emit result from index k − 1 onward (first full window).
Edge cases to catch in interview
- k = 1 → every element is its own window max.
- k = n → single answer = max of array.
- Negative numbers — unchanged logic.
Examples
Example 1
[3, 3, 5, 5, 6, 7]{"nums":[1,3,-1,-3,5,3,6,7],"k":3,"values":[1,3,-1,-3,5,3,6,7]}
Example 2
[1]{"nums":[1],"k":1,"values":[1]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed