← Queues · guided lesson
Queues & Deques
the story
Queue (FIFO)
Every tool here exists because something was too slow or broke without it. Here is what.
Nobody invented "first come, first served" — waiting lines are older than computers, and the FIFO queue in code has no single inventor. What has a clear start is the maths of queues. In 1909 Erlang, an engineer at the Copenhagen Telephone Company, showed that calls made at random follow a Poisson distribution. His 1917 paper gave formulas for lost calls and waiting time, and telephone companies in many countries adopted them. That field, queueing theory, is still how engineers size server pools, call centres and network buffers.
the problem it solved
How many lines a telephone exchange needs so that calls rarely wait or get lost — sizing any system that serves requests in arrival order.
why it matters: without it
Bursts of work would be dropped or served in random order — no fairness for print jobs or bookings — and BFS would stop finding shortest paths.
sources: MacTutor History of Mathematics — Agner Krarup Erlang (University of St Andrews) ↗
also in this chapter: Circular buffer (ring buffer) · Deque and Java's ArrayDeque · Monotonic deque · Queue from two stacks (amortized O(1))
the idea
The idea in 30 seconds
A queue lets you add at the rear and remove from the front: whoever arrived first leaves first (FIFO).
Both ends must be O(1). An array does that by letting head and tail walk forward and wrap around; a linked list does it with head and tail pointers.
A deque ("deck") opens both ends. Keep it sorted as you go and it becomes a monotonic deque — the trick behind sliding-window maximum.
picture this
Your printer's queue: jobs wait in the order you pressed Ctrl+P. The printer always takes the oldest job; a new job joins at the back. No job jumps the line — that is FIFO.
watch it
Linked queue: enqueue & dequeue
Predict mode is on. Before each step, guess what happens, then watch. Wrong guesses are fine; that is how it sinks in.
Linked queue: enqueue & dequeue
Top: the queue as the user sees it. Bottom: the linked nodes in memory, with head and tail markers.
On this screen ← / → step the simulation. Use the buttons below to move through the lesson.
watch it
Circular buffer: watch head and tail wrap around
Predict mode is on. Before each step, guess what happens, then watch. Wrong guesses are fine; that is how it sinks in.
Circular buffer: watch head and tail wrap around
The array never shifts. Grey slots are free (or stale); the live part can wrap past the end back to index 0.
On this screen ← / → step the simulation. Use the buttons below to move through the lesson.
key idea 1 of 8
Read it once. Say it out loud once. Bas.
FIFO: the element removed is always the one that has waited longest. A stack is LIFO; a queue is its opposite.
key idea 2 of 8
Read it once. Say it out loud once. Bas.
Circular array: tail = (tail + 1) % capacity and head = (head + 1) % capacity. Nothing ever shifts, so enqueue and dequeue are O(1).
key idea 3 of 8
Read it once. Say it out loud once. Bas.
Track size (or keep one slot empty) — otherwise head == tail can't tell an empty queue from a full one.
key idea 4 of 8
Read it once. Say it out loud once. Bas.
`ArrayDeque` is Java's default Queue and Stack: fast, cache-friendly, amortized O(1) at both ends. No null elements allowed.
key idea 5 of 8
Read it once. Say it out loud once. Bas.
`offer` / `poll` / `peek` return false/null on failure; `add` / `remove` / `element` throw. Pick one family and stay consistent.
key idea 6 of 8
Read it once. Say it out loud once. Bas.
Monotonic deque: pop from the back every element that can never be the answer again. Each index enters and leaves once → O(n) total.
key idea 7 of 8
Read it once. Say it out loud once. Bas.
Two stacks make a queue with amortized O(1) per operation: pour in into out only when out is empty.
key idea 8 of 8
Read it once. Say it out loud once. Bas.
Queues power BFS: processing nodes in arrival order visits them in order of distance — you'll use this in every graph chapter.
spot the pattern
When you see this, think this
"Process in the order they arrive", "first come first served", task scheduling
Plain FIFO queue (ArrayDeque)
Shortest path / fewest steps in an unweighted graph or grid, level-by-level
BFS with a queue
"Within the last X seconds / last k items", stream of timestamps
Queue as a time window: offer new, poll expired from the front
Max or min of every window of size k
Monotonic deque of indices
+4 more signals on the chapter page
watch out
A trap or two
Many wrong answers in interviews come from small slips like these, not from the hard part.
- Using
head == tailalone to test empty/full in a circular array — they look identical. Keep asizecounter. - Computing
(head - 1) % nin Java:%keeps the sign, so-1 % 5 == -1. Use(head - 1 + n) % n.
quick check
One question. No pressure.
Circular queue with capacity 5, tail = 4. You enqueue one element. What is tail now?
now try
Your first real problem
You know enough to try this one. Stuck? Use the hints; that is what they are for.
Implement Queue using Stacks
open the problem →recall
Flip a few cards
Answer in your head first, then flip. Grade yourself honestly; the schedule brings back what you forget.
Queue vs stack: which element comes out next?
Question 1: Queue vs stack: which element comes out next?
done
Lesson complete
Chalo, that is the core. The full chapter has the rest: cost tables, Java details and more problems.