level 4 · Linear structures
Queue
First in, first out. Fairness, as a data structure.
what is it
Start here
A queue is a line. You join at the back, you get served from the front, and nobody cuts. The item that has been waiting longest is always the next one out.
It is the exact mirror image of a stack. A stack is last-in-first-out; a queue is first-in-first-out. Swapping which end you take from completely changes what the structure is *for*.
Queues show up wherever fairness or order matters: print jobs, customer support tickets, tasks waiting for a CPU, messages waiting to be sent. And later, when you learn breadth-first search on a graph, a queue is the thing that makes it explore level by level instead of diving deep.
real-life analogy
Picture it
You arrive, you go to the back. The person at the front — who has waited longest — gets served and leaves. Everyone shuffles forward one place. If someone could take from the middle, it wouldn't be a queue, it would be a riot.
interactive visualization
Watch it run
These join the line one by one. Then two get served.
- nobody waiting
A queue is the line at a ticket counter. You join at the back, and you get served from the front. No cutting.
step 01/10
- comparing
- moving
space · play ← → · step
| 1 | class Queue { |
| 2 | constructor() { this.items = []; } |
| 3 | enqueue(value) { |
| 4 | this.items.push(value); |
| 5 | } |
| 6 | dequeue() { |
| 7 | if (this.isEmpty()) return null; |
| 8 | return this.items.shift(); |
| 9 | } |
| 10 | front() { |
| 11 | return this.items[0]; |
| 12 | } |
| 13 | isEmpty() { |
| 14 | return this.items.length === 0; |
| 15 | } |
| 16 | } |
variables right now
- size
- 0
the dry run · every step, in words
10 stepscomplexity
What it costs
- best case
- O(1) enqueue
- average
- O(1) dequeue
- worst case
- O(n) if built badly
- extra memory
- O(n)
Both operations should be O(1). But if you build a queue on a plain array and dequeue with shift(), every removal shuffles the whole array forward — quietly turning an O(1) operation into O(n). Use a proper deque.
- O(1) · this one
- O(log n)
- O(n)
- O(n log n)
- O(n²)
common mistakes
Common traps
Building a queue on an array and using shift() to dequeue.
shift() moves every remaining element left. On a big queue that's O(n) per removal. Use a linked list or a deque, where the front is just a pointer.
Mixing up which end is which.
Add at the back, remove from the front. Do both at the same end by accident and you've built a stack.
Dequeuing from an empty queue.
Same trap as an empty stack. Check first — an empty line has nobody to serve.
quiz
Check yourself
Three questions. Get them all right to finish the lesson.
+50 XP01You enqueue 1, 2, 3, then dequeue once. What comes out?
02What's the real difference between a stack and a queue?
03Why can dequeuing from an array-based queue be slow?
practice
Solve it on LeetCode
You've seen it run — now write it yourself. These are real LeetCode problems that use exactly this idea, from gentlest to toughest.