What is Stack?
Stack is a LIFO (Last In, First Out) data structure where elements are added and removed from the same end. It’s perfect for tracking “most recent” state.Quick Recognition: Problems involving matching pairs, nested structures, “previous/next greater element”, or undo operations.
Pattern Recognition Checklist
Use Stack When
- Matching parentheses/brackets/tags
- Evaluating expressions (postfix/infix)
- Finding next/previous greater/smaller
- Processing nested structures
- Implementing undo/back operations
Don't Use When
- Need random access to elements
- FIFO ordering needed (use Queue)
- Need to process from both ends
- Simple array traversal suffices
Stack Type Quick Reference
| Problem Type | Stack Type | Direction |
|---|---|---|
| Next Greater Element | Monotonic Decreasing | Left to Right |
| Previous Greater Element | Monotonic Decreasing | Left to Right |
| Next Smaller Element | Monotonic Increasing | Left to Right |
| Largest Rectangle | Monotonic Increasing | Left to Right |
| Trapping Rain Water | Either | Two-pointer or stack |
When to Use
Matching Problems
Parentheses, tags, nested structures
Expression Evaluation
Infix, postfix, prefix expressions
Monotonic Stack
Next greater/smaller element
Undo Operations
Browser history, text editors
Pattern Variations
1. Valid Parentheses
2. Decode String
3. Basic Calculator
4. Min Stack
5. Daily Temperatures (Monotonic Stack)
Classic Problems
1. Valid Parentheses
1. Valid Parentheses
Pattern: Push opening, pop and match closingKey: Stack should be empty at end for valid input
2. Largest Rectangle in Histogram
2. Largest Rectangle in Histogram
Pattern: Monotonic increasing stackKey: Pop and calculate area when current bar is shorter
3. Evaluate Reverse Polish Notation
3. Evaluate Reverse Polish Notation
Pattern: Push numbers, pop two on operatorKey: Order matters for subtraction/division
4. Simplify Path
4. Simplify Path
Pattern: Split by ’/’, stack for directory navigationKey: ’..’ pops, ’.’ and ” are ignored
Monotonic Stack Deep Dive
Common Mistakes
Debugging Checklist
1
Check Empty Stack
Always verify
!stack.empty() before peek() or pop()2
Verify Matching Logic
For parentheses: opening pushes, closing pops and compares
3
Check Remaining Elements
After loop, stack may still have unprocessed elements
4
Validate Monotonic Direction
Increasing: pop smaller, Decreasing: pop larger
5
Track Indices vs Values
For distance calculations, store indices not values
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Valid Parentheses | All FAANG | Push open, pop close |
| Baseball Game | Amazon | Stack operations |
| Remove Outer Parens | Counter or stack | |
| Backspace String | Stack simulation |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “This is a matching problem, so I’ll use a stack.”
- “Opening brackets push, closing brackets pop and compare.”
- “If match fails or stack has leftovers, it’s invalid.”
- “For monotonic: I maintain a stack where elements are always sorted.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Valid parentheses/brackets” | Basic stack push/pop |
| ”Next greater element” | Monotonic decreasing stack |
| ”Largest rectangle” | Monotonic increasing stack |
| ”Evaluate expression” | Two stacks (operand + operator) |
| “Simplify path” | Stack for directory navigation |
Monotonic Stack Template
Monotonic Stack Template
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Valid Parentheses | Easy | LeetCode 20 |
| Min Stack | Medium | LeetCode 155 |
| Daily Temperatures | Medium | LeetCode 739 |
| Largest Rectangle in Histogram | Hard | LeetCode 84 |
| Basic Calculator | Hard | LeetCode 224 |
Practice Roadmap
1
Day 1: Basic Stack
- Solve: Valid Parentheses, Min Stack
- Focus: Push, pop, peek operations
2
Day 2: Expression Evaluation
- Solve: Evaluate RPN, Decode String
- Focus: Operand vs operator stacks
3
Day 3: Monotonic Stack
- Solve: Next Greater Element, Daily Temperatures
- Focus: When to pop from stack
4
Day 4: Hard Problems
- Solve: Largest Rectangle, Basic Calculator
- Focus: Complex monotonic patterns