← All patterns
9 / 32
Stacks, Queues & Heaps

Queue

When you see it

Signal words

If the prompt uses any of these, this pattern should come to mind first.

FIFOlevel by levelfirst come first servedmoving averagerecent counter
Practice

Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.

Ask yourself

  1. Process in arrival order (FIFO)?
  2. Sliding max/min over a stream → monotonic deque.
  3. Often the engine inside BFS.

Classic problem

FIFO Queue — A queue follows First-In-First-Out order: enqueue appends at the back, dequeue removes from the front. Implemented with an array pointer or linked list.

Input: ops = [enqueue(2), enqueue(5), enqueue(7), dequeue(), enqueue(9), dequeue(), dequeue()]

Output: dequeue order: 2, 5, 7 — values leave in the order they arrived.

Watch it run

Common pitfalls

Using a list .pop(0) — O(n); use a deque.

Forgetting to mark visited before enqueue (dupes).