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
- Process in arrival order (FIFO)?
- Sliding max/min over a stream → monotonic deque.
- 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.
Common pitfalls
Using a list
.pop(0)— O(n); use a deque.
Forgetting to mark visited before enqueue (dupes).