ReviseAlgo Logo
EasyHigh Frequency15 min

Stack Fundamentals

Stack operations, call stack visualization, and when to reach for a stack.

Stack = LIFO. Use it when you need to remember context while going forward: matching brackets, undo operations, DFS, expression evaluation.

Stack — LIFO Structure

Operation Complexities

Operation
Time
Note
push(x)
O(1)
Add to top — amortized O(1) with dynamic array
pop()
O(1)
Remove from top
peek()
O(1)
View top without removing
isEmpty()
O(1)
Check size == 0
search(x)
O(n)
No O(1) search — must iterate

Operations — Step by Step

Bracket Matching

O(n)Push opens; pop on close; validate match; return isEmpty()
Step-by-step1 / 5

Input stream

{
0
[
1
]
2
}
3

Invariant

Push work onto the stack; pop when the current token or base case resolves it.

Stacktop
{

See "{" → push

Opening bracket. Push "{". Stack = [{].