Skip to content
DSA Patterns
Menu
Language

Pattern #11

Queue & Deque

Recommended

FIFO 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/8

Sliding window maximum with k = 3. Deque holds candidates decreasing.

1
0
3
1
-1
2
-3
3
5
4
3
5
6
6
7
7
stack
∅
Statek=3dq=[]

front = current max index

Key:CurrentActive rangeMatchSettledEliminated

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

  1. 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).
  2. Evict before you read the answer. The front of LC 239 must still lie inside [i - k + 1, i].
  3. 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 to t - 3000.
  4. A ring stores head and count, not a tail pointer. Full is count == k. The deque steps head backward on insertFront (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;
}
#StatusProblemTypeDone
  1. 1#239 Sliding Window MaximumGuide
  2. 2#346 Moving Average from Data StreamGuide
  3. 3#362 Design Hit CounterGuide
  4. 4#622 Design Circular QueueGuide
  5. 5#641 Design Circular DequeGuide
  6. 6#933 Number of Recent CallsGuide