Stack
Stacks provide Last-In-First-Out (LIFO) access, making them ideal for problems involving nested structures, parsing, backtracking, and maintaining context while processing sequences.Key Patterns & Techniques
1. Monotonic Stack
- Maintain elements in increasing/decreasing order
- Find next greater/smaller element
- Used in temperature, stock span problems
2. Expression Evaluation
- Parse and evaluate mathematical expressions
- Handle operator precedence
- Postfix, infix, prefix conversion
3. Matching & Validation
- Validate balanced parentheses
- Match opening and closing symbols
- HTML/XML tag validation
4. String Decoding
- Decode nested encoded strings
- Handle nested brackets with recursion or stack
- Build result string incrementally
Common Approaches
Basic Calculator Pattern
Valid Parentheses Pattern
Decode String Pattern
Monotonic Stack Pattern
Problems in This Category
008. Basic Calculator II
Medium | Frequency: 84.0%Stack-based calculator handling +, -, *, / with proper precedence.
093. Decode String
Medium | Frequency: 32.0%Decode nested encoded strings using stack to track context.
035. Simplify Path
Medium | Frequency: 65.0%Unix path simplification using stack for directory navigation.
049. Exclusive Time of Functions
Medium | Frequency: 56.0%Stack to track function call hierarchy and compute exclusive times.
098. Remove All Adjacent Duplicates in String II
Medium | Frequency: 32.0%Stack with counts to remove k consecutive duplicate characters.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | When to Use |
|---|---|---|---|
| Basic Stack | O(n) | O(n) | Nested structures, validation |
| Monotonic Stack | O(n) | O(n) | Next greater/smaller element |
| Calculator | O(n) | O(n) | Expression evaluation |
| String Decode | O(n * k) | O(n) | Nested encoding, k is repeat count |
| Path Simplification | O(n) | O(n) | Directory navigation |
Interview Tips
Pattern Recognition
- “Balanced parentheses/brackets” → Stack for matching
- “Evaluate expression” → Stack for operators/operands
- “Next greater/smaller” → Monotonic stack
- “Decode nested string” → Stack to track context
- “Simplify/parse path” → Stack for directory levels
- “Remove adjacent duplicates” → Stack with counts
Common Mistakes
- Not handling operator precedence correctly (*, / before +, -)
- Forgetting to process remaining stack elements
- Off-by-one errors when checking stack.peek()
- Not handling spaces in calculator problems
- Wrong division truncation (Python // vs int(/))
Key Insights
- When to push? When you need to remember context (opening bracket, operator)
- When to pop? When closing a context (closing bracket) or found better element
- What to store? Often indices instead of values for result building
- Monotonic invariant makes O(n) possible - each element pushed/popped once
- Calculator trick high precedence ops (*, /) modify stack top immediately
Stack Variations
Min/Max Stack
Stack with Counts
Decision Tree
Advanced Patterns
Two Stacks for Queue
- Implement queue using two stacks
- Amortized O(1) for enqueue/dequeue
Stack for DFS
- Iterative DFS using explicit stack
- Avoids recursion depth limits
- Same logic as recursive DFS
Expression Trees
- Build tree from expression using stack
- Used in compilers and calculators
Pro Tip: If you see nested structures (parentheses, tags, directories), think stack. If you need “next greater/smaller”, use monotonic stack for O(n) solution instead of O(n²) brute force.