DSA Guide
Stacks & Queues

Stacks & Queues

Last-in-first-out and first-in-first-out — and how to recognise which one a problem needs

Stacks and queues are the same idea — a sequence you add to and remove from — differing only in which end you remove from. That single choice determines which problems they solve.

What They Actually Look Like

Both are just a list with a rule about where you are allowed to touch it. The rule is the entire point: by giving up random access, you get a structure whose behaviour matches how certain problems are shaped.

The names

WordStackQueue
Add an itempushenqueue
Remove an itempopdequeue
Look without removingpeek / topfront
OrderLIFO — last in, first outFIFO — first in, first out

Everyday version: a stack is a pile of plates — you take from the top, which is the one added most recently. A queue is a line at a shop — first to arrive is first served.

A stack, operating

Everything happens at one end. The bottom of the pile is untouchable until everything above it has gone.

Push 1, 2, 3 — then pop twice
Stack (top first)
1
operationpush(1)top1
Push 1. It sits on the bottom.
1 / 5
Pushed 1, 2, 3 → popped 3, 2. Reversal is free, and that is what makes stacks good at undo and nesting.

A queue, operating

Two ends. You add at the back and remove from the front, so order is preserved instead of reversed.

Enqueue 1, 2, 3 — then dequeue twice
Queue (front → back)
1
operationenqueue(1)front1
1 joins the empty queue.
1 / 5
Enqueued 1, 2, 3 → dequeued 1, 2. Order is kept, which is what makes queues find shortest paths.

Why this one difference matters so much

Put the same three items through both and you get opposite outputs — 3, 2, 1 versus 1, 2, 3.

That is the whole reason DFS and BFS are different algorithms. They share identical code; one holds pending work in a stack, the other in a queue. Serving newest-first makes you dive; serving oldest-first makes you sweep.

One line of difference, two completely different behaviours.

Stack — Last In, First Out

Reach for a stack whenever the most recent thing is the first thing you need to resolve.

Signals that a stack is the answer

Queue — First In, First Out

Reach for a queue when things must be processed in arrival order.

Do not use a plain list as a queue

Python: list.pop(0) is O(n) because everything shifts left. Use collections.deque, whose popleft is O(1).

TypeScript: Array.prototype.shift() has the same cost. Either keep a read index into the array, or build a fresh array per BFS level.

Getting this wrong silently turns an O(n) traversal into O(n²).

Augmented Stacks

A stack can carry extra state alongside its values, giving O(1) answers to questions that would otherwise need a scan.

  • Min Stack — every entry stores the minimum as of its push

The same idea powers monotonic stacks, which keep their contents sorted and solve "next greater element" style problems in linear time.

Language Notes

OperationPythonTypeScript
Pushstack.append(x)stack.push(x)
Popstack.pop() — raises on emptystack.pop() — returns undefined
Peekstack[-1]stack[stack.length - 1]
Enqueuequeue.append(x)queue.push(x)
Dequeuequeue.popleft() (deque)index-based read, or swap arrays

That difference in pop behaviour on an empty container shows up repeatedly: Python needs an explicit emptiness check where TypeScript's undefined falls through harmlessly.

All Problems

ProblemDifficultyPattern
Valid ParenthesesEasyStack
Min StackMediumAugmented Stack
Evaluate Reverse Polish NotationMediumStack
Simplify PathMediumStack

On this page