Skip to main content
CodeFlow
data structures~12 minRoute 02 4 of 9

The Queue

First in, first out — the line at the chai stall.

Prerequisites:The Stack

Story — Queue the Barista

chaibaristafrontrearfirst in, first out

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

Two ends, two jobs. New folks line up at the back; I serve from the front. Don't ask me to grab someone in the middle — that's a different job, for a different data structure.

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.

← dequeueenqueue ←

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?

← dequeueenqueue ←
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.

  1. queue.append("B")
  2. from collections import deque
  3. front = queue.popleft()
  4. queue = deque()
  5. queue.append("A")
Hint: Import first, then create the queue, then add items, then take from the front.

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

Step 1 / 5python
1from collections import deque
2queue = deque()
3queue.append("A")
4queue.append("B")
5front = queue.popleft()
6print(queue)

Quiz it — make it stick

  1. Question 1

    You enqueue X, Y, Z in that order, then dequeue once. What did you dequeue?

  2. Predict — Question 2

    What does this print?

    from collections import deque
    q = deque()
    q.append(1)
    q.append(2)
    q.append(3)
    q.popleft()
    q.append(4)
    print(list(q))
  3. True or false — Question 3

    Using a Python list as a queue with `pop(0)` is just as fast as using `collections.deque`.

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.