QueuesLesson 1

How Queues Work

First in, first out (FIFO). Enqueue, dequeue, and peek — what a queue is and where it appears in real systems.

What Is It?

A queue is a data structure that works like a line at a coffee shop. The first person who joins the line is the first person who gets served. That rule has a name: FIFO — First In, First Out.

A queue has four operations:

  1. enqueue(value) — Add an element to the back of the queue.
  2. dequeue() — Remove and return the element from the front of the queue.
  3. peek() — Look at the front element without removing it.
  4. isEmpty() — Check if the queue has no elements.

You add to one end and remove from the other. That two-ended rule is what makes a queue different from a stack.

pseudocode
1
2
3
4
5
6
7
8
enqueue(40)
|
v
Back Front
+------+------+------+------+
| 40 | 30 | 20 | 10 | --> dequeue() returns 10
+------+------+------+------+
rear front

New elements join at the back. Removal happens at the front. Element 10 was added first and will leave first.

Analogy

Picture the checkout line at a grocery store. Customers join at the back and leave from the front after being served. The first person in line is the first person served — that is FIFO.

Think of yourself as the store manager:

  • enqueue: A new customer joins the back of the line.
  • dequeue: The cashier serves and removes the customer at the front.
  • peek: You glance at who is at the front without serving them yet.
  • isEmpty: You check if anyone is waiting at all.

No cutting in line. No serving the last person first. Strict order: first come, first served. That is the queue rule.

Try It Yourself

front →
10
20
30
← rear

How It Works

FIFO Means Order Is Preserved

If you add A, B, C, D to a queue and then remove four times, you get A, B, C, D — the same order you put them in.

Compare that to a stack (LIFO — Last In, First Out). Push A, B, C, D onto a stack, then pop four times, and you get D, C, B, A — the reverse order.

pseudocode
1
2
Queue: enqueue A, B, C, D -> dequeue: A, B, C, D (same order)
Stack: push A, B, C, D -> pop: D, C, B, A (reversed)

This difference determines which one you use.

The Four Operations Up Close

enqueue(value) adds to the rear:

pseudocode
1
2
3
4
5
Before enqueue(40):
Front [10] [20] [30] Rear
After enqueue(40):
Front [10] [20] [30] [40] Rear

dequeue() removes from the front. If the queue is empty, this is an error:

pseudocode
1
2
3
4
5
Before dequeue():
Front [10] [20] [30] [40] Rear
After dequeue() -> returns 10:
Front [20] [30] [40] Rear

peek() returns the front element without removing anything:

pseudocode
1
2
3
4
peek() on:
Front [10] [20] [30] Rear
Returns: 10 (queue is unchanged)

isEmpty() returns true if there are no elements. Always check this before calling dequeue() or peek() — calling either on an empty queue causes an error.

Where Queues Show Up in Real Life

Print queue: When you send three documents to a printer, they go into a queue. The first document sent gets printed first.

Task scheduling: Operating systems use queues to decide which program gets to run next. Programs wait in a queue and get their turn in order.

Breadth-First Search (BFS): This is the big one for coding interviews. BFS uses a queue to explore a graph or tree level by level. You add a node's neighbors to the back of the queue, then process nodes from the front. This guarantees you visit every node at distance 1 before any node at distance 2.

pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BFS on a tree:
A
/ \
B C
/ \
D E
Queue trace:
enqueue A -> [A]
dequeue A, enqueue B, C -> [B, C]
dequeue B, enqueue D, E -> [C, D, E]
dequeue C -> [D, E]
dequeue D -> [E]
dequeue E -> []
Visit order: A, B, C, D, E (level by level)

Message queues: In real software systems, services talk to each other by passing messages through a queue. Messages are processed in the order they were sent.

Tracing Queue Operations

Queue Operations TraceFront is on the left
1 / 14
enqueue(10)
10
[0]

Notice: the dequeue order (10, 20, 30, 40, 50) matches the enqueue order exactly. That is FIFO.

Queue vs. Stack: When to Use Which

Use a Queue when

  • First-come-first-served processing
  • Level-by-level traversal (BFS)
  • Scheduling (tasks, prints, processes)
  • Buffering (streaming data)
  • Message passing between systems

First In, First Out

Use a Stack when

  • Last-in-first-out processing
  • Depth-first traversal (DFS)
  • Undo/redo operations
  • Expression evaluation/parsing
  • Backtracking (maze, recursion)

Last In, First Out

The key question: Does the most recent item need to be processed first (stack) or should items be processed in arrival order (queue)?

Examples

Example 1: Customer Service Simulation

pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
queue = new Queue()
// Customers arrive
enqueue(queue, "Alice") // [Alice]
enqueue(queue, "Bob") // [Alice, Bob]
enqueue(queue, "Charlie") // [Alice, Bob, Charlie]
// Serve customers
serve(dequeue(queue)) // Serves Alice -> [Bob, Charlie]
serve(dequeue(queue)) // Serves Bob -> [Charlie]
// More arrive
enqueue(queue, "Diana") // [Charlie, Diana]
serve(dequeue(queue)) // Serves Charlie -> [Diana]
serve(dequeue(queue)) // Serves Diana -> []

Customers are served in arrival order no matter when new customers join.

Example 2: Hot Potato Game

N people stand in a circle and pass a "hot potato." Every k-th person is eliminated. You can simulate this with a queue: dequeue and re-enqueue k-1 times, then dequeue the k-th person (they are eliminated). Repeat until one person remains.

Common Mistakes

1. Dequeuing from an empty queue. Always call isEmpty() before calling dequeue() or peek(). Calling either on an empty queue is a runtime error.

2. Confusing FIFO with LIFO. If your results come out in reversed order, you are using stack behavior instead of queue behavior. Double-check which end you are adding to and removing from.

3. Enqueuing at the wrong end. Enqueue always goes to the rear. Dequeue always comes from the front. Swapping them breaks FIFO.

4. Forgetting that dequeue removes the element. Unlike peek, dequeue is destructive. If you need the value but also need it to stay in the queue, use peek.

Best Practices

Always check isEmpty before dequeue or peek. This prevents crashes.

Use a queue any time order of arrival matters. Task queues, print queues, message queues — whenever things need to be processed in the order they arrived, use a queue.

Know that BFS uses a queue. This is the most important use of queues in coding interviews. Shortest path in an unweighted graph? Level-order traversal of a tree? The answer involves a queue.

Understand queue vs. stack deeply. Queue preserves order. Stack reverses order. Many interview problems test whether you can pick the right one.