The Queue
First in, first out — the line at the chai stall.
Prerequisites:The Stack
Story — Queue the Barista
Picture the chai stall on a Monday morning. People walk up, stand at the back of the line, and wait. The barista — Queue, by name — only ever serves the person at the very front. The first person to arrive is the first person to get a cup. No skipping, no shortcuts.
That rule has a name: FIFO — first in, first out. New customers join the rear; the barista pulls from the front. That's a queue. Every queue in programming, anywhere, follows this rule.
Queue says
See it — enqueue, dequeue, peek
Type a value and press Enqueue to add a customer to the rear, or Dequeue to serve the one at the front. Watch the front and rear pointers shift as people come and go.
Try it — predict the order
Exercise. Enqueue A, B, C, D in that order. Dequeue twice, then enqueue E. Now dequeue everything, one at a time. In what order do the values come out?
Reveal answer
After enqueuing A, B, C, D the queue is [A, B, C, D]. Two dequeues remove A and B (in that order). Enqueue E gives [C, D, E]. Dequeueing everything yields C, D, E — same order they were added. That is what FIFO means.
Now reorder these scrambled lines into a working queue program:
Drag the pieces — or tab to one and use the ↑/↓ buttons — to put the program in order.
queue.append("B")from collections import dequefront = queue.popleft()queue = deque()queue.append("A")
Code it — five languages, one line
Every language has its own name for the same two operations: add to the rear, take from the front. Python uses append / popleft; Java says offer / poll; C++ has push / pop. Different vocabulary, identical behaviour.
from collections import deque
queue = deque()
queue.append("A")
queue.append("B")
front = queue.popleft() # "A"
print(queue) # deque(["B"])Walk through it line by line
1from collections import deque2queue = deque()3queue.append("A")4queue.append("B")5front = queue.popleft()6print(queue)
Quiz it — make it stick
No Dumb Questions
Real questions other learners asked on this page.
How is a queue different from a stack?
They use the same operations names in different languages, but the rule is opposite. A stack is LIFO — the last thing in is the first thing out (think pancakes). A queue is FIFO — the first thing in is the first thing out (think a line at a counter). Same storage, different etiquette.Where do real programs use queues?
Almost anywhere "tasks waiting in order" appears: print jobs lining up at a printer, messages waiting in a chat server, a web server processing requests, BFS (breadth-first search) on a graph. The "task queue" you see in distributed systems (Celery, BullMQ, AWS SQS) is exactly this idea, scaled up.What is a "circular queue" — and why?
When you implement a queue with a fixed-size array, dequeueing leaves a gap at the front. Rather than shift everyone left, you wrap the rear pointer around to the freed slot — the array becomes a circle. It saves the O(n) shift and is what every operating system uses for buffer queues.What is a priority queue? Is it still FIFO?
No — that is the whole point. A priority queue picks the next item by importance, not by arrival order. A VIP at the chai stall jumps the line. Most programs use a heap to implement it, which gets you "next-most-important" in log time. We cover heaps in a later concept.