Pattern #11
Queue & Deque
RecommendedFIFO processing, sliding-window extrema, and recent-event tracking.
When to use
Order of processing matters (BFS-like), or you need min/max in a moving window in amortized O(1).
Recognition cues
- Sliding window maximum
- Moving average / hit counter
- Design circular queue / recent calls
Common pitfalls
- Forgetting to evict indices that left the window
- Using a list pop(0) in Python (O(n)) instead of deque
- Off-by-one on inclusive window bounds
90-second recognition drill
Which pattern fits best?
- Sliding window maximum
- Moving average / hit counter
- Design circular queue / recent calls
Interactive
Mental model
A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.
Step 1 of 8
1
03
1-1
2-3
35
43
56
67
7stack
∅
Statek=3dq=[]
front = current max index
How to think about it
A queue preserves arrival order. A monotonic deque stores candidate indices for window min/max: front is always the answer; back drops dominated values. Combined with a sliding window, each index enters and leaves once.
Template shapes
| Shape | Core move | Example |
|---|---|---|
| Window maximum | Deque of indices, values decreasing toward the back. Drop the front once it is older than i - k. Pop the back while it is ≤ nums[i], then push i. After i ≥ k - 1, the front is the max |
LC 239 |
| Moving average | FIFO of the last size values plus a running sum. Past size, subtract and drop the front. next returns sum / count |
LC 346 |
| Recent timestamps | Enqueue t. LC 362 drops the front while front ≤ t - 300. LC 933 drops it while front < t - 3000. Timestamps only increase, so only the front expires |
LC 362, LC 933 |
| Circular queue | head and count, capacity k. Enqueue writes at (head + count) % k. Empty is count == 0 and full is count == k |
LC 622 |
| Circular deque | The rear sits at (head + count - 1) % k. insertLast writes at (head + count) % k. insertFront steps head backward one slot |
LC 641 |
Complexity baseline
O(n) amortized for window extrema; O(1) per op for the queue and deque designs.
From template to problem
- A window maximum needs both ends, so it is a deque of indexes (LC 239). A moving average only drops the front, so a FIFO is enough (LC 346).
- Evict before you read the answer. The front of LC 239 must still lie inside
[i - k + 1, i]. - Timestamps on this path only increase. Drop the front while it is stale, and do not scan the rest (LC 362, LC 933). LC 362 drops a timestamp equal to
t - 300. LC 933 keeps a timestamp equal tot - 3000. - A ring stores
headandcount, not a tail pointer. Full iscount == k. The deque stepsheadbackward oninsertFront(LC 641).
Template
Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.
Queue & Deque · Template
/** Deque template: sliding window maximum. */
export function maxSlidingWindow(nums: number[], k: number): number[] {
const dq: number[] = [];
const res: number[] = [];
for (let i = 0; i < nums.length; i++) {
while (dq.length && dq[0]! <= i - k) dq.shift();
while (dq.length && nums[dq[dq.length - 1]!]! <= nums[i]!) dq.pop();
dq.push(i);
if (i >= k - 1) res.push(nums[dq[0]!]!);
}
return res;
}
/** Deque template: sliding window maximum. */
export function maxSlidingWindow(nums: number[], k: number): number[] {
const dq: number[] = [];
const res: number[] = [];
for (let i = 0; i < nums.length; i++) {
while (dq.length && dq[0]! <= i - k) dq.shift();
while (dq.length && nums[dq[dq.length - 1]!]! <= nums[i]!) dq.pop();
dq.push(i);
if (i >= k - 1) res.push(nums[dq[0]!]!);
}
return res;
}
#StatusProblemTypeDifficultyDone
- 1#239 Sliding Window MaximumGuidehard
- 2#346 Moving Average from Data StreamGuideeasy
- 3#362 Design Hit CounterGuidemedium
- 4#622 Design Circular QueueGuidemedium
- 5#641 Design Circular DequeGuidemedium
- 6#933 Number of Recent CallsGuideeasy