What a stack is
This section has a step-by-step animation in the interactive lesson.
Quick check
You push 3, then 7, then 5 onto a stack. Which value is on top?
Answer it in the interactive lesson to keep your progress.
The core operations
[3, 7] (3 at the bottom, 7 on top) and watch each one:- push(5) — add 5 to the top. Stack becomes
[3, 7, 5]. - pop() — remove and return the top. Returns 5, stack goes back to
[3, 7]. - peek() — look at the top without removing it. Returns 7, stack stays
[3, 7]. - isEmpty() — is there anything here?
[3, 7]→ false;[]→ true. - size() — how many items?
[3, 7]→ 2.
Quick check
Stack is [3, 7] (7 on top). You call peek(), then pop(). Predict what pop() returns.
Answer it in the interactive lesson to keep your progress.
Your first stack problem: reverse a word
CAT into TAC. A stack makes this almost too easy, because its one rule (last-in, first-out) is reversal. The plan: drop every letter onto the stack in order, then take them all back off.The steps
- Start with an empty stack and an empty result.
- Push each item of the input onto the stack, left to right.
- While the stack isn't empty, pop the top item and append it to the result.
- The result now holds the items in reverse order.
Trace it on CAT
| step | action | stack | result |
|---|---|---|---|
| 1 | push C, A, T | [C, A, T] | "" |
| 2 | pop → T | [C, A] | "T" |
| 3 | pop → A | [C] | "TA" |
| 4 | pop → C | [] | "TAC" |
O(n) — linear time (from the Complexity lesson: the work grows in step with the input's length). In JavaScript, one way to write those exact steps:function reverse(word) {
const stack = [];
for (const ch of word) stack.push(ch); // push every letter
let out = '';
while (stack.length) out += stack.pop(); // pop them back — reversed
return out;
}
console.log(reverse('CAT')); // 'TAC'Quick check
Why does pushing then popping every letter reverse the word?
Answer it in the interactive lesson to keep your progress.
Where it breaks (and how not to get burned)
- Popping an empty stack. In JavaScript
[].pop()returnsundefined— it does not throw. If your code trusts that value, it may silently do the wrong thing. Guard with anisEmpty/ length check before you pop when the stack might be empty. - Using pop() when you meant peek(). If you only want to look at the top (say, to compare it),
pop()will remove it and corrupt your stack. Usestack[stack.length - 1]to read without removing.
Quick check
In JavaScript, calling .pop() on an EMPTY array…
Answer it in the interactive lesson to keep your progress.
How fast is it? (complexity)
| Operation | Cost | Why |
|---|---|---|
| push | O(1) | drop onto the top slot |
| pop | O(1) | lift off the top slot |
| peek / isEmpty / size | O(1) | read-only glances |
n items uses O(n) space.- A stack is LIFO: last in, first out, all action at the top.
- Two movers — push and pop; three readers — peek, isEmpty, size.
- Every operation is O(1), constant time.
- Reach for a stack whenever the most recent thing is what you handle next.
What's next
- Undo history — every action is pushed on; 'undo' pops the most recent one off.
- Balanced brackets — push each opener; a closer must match the top of the stack.
- Min-stack — a stack that also returns its smallest value in
O(1). - Monotonic stack — the pattern behind 'next greater element'.