Stack
Understand LIFO data structure and its applications in parsing and backtracking.
7
Problems
0
Easy
7
Medium
0
Hard
How Stack Works
A Stack follows Last-In-First-Out (LIFO) ordering, making it perfect for problems involving matching, nesting, or "most recent" lookups. The Monotonic Stack variant maintains elements in sorted order, enabling efficient "next greater/smaller element" queries. When you push an element, you first pop all elements that violate the monotonic property, processing them as you go. This processes each element at most twice (push + pop), giving O(n) total time.
When to Use Stack
Pattern Recognition
Look for these trigger words in problem statements:
valid parentheses
stack
min stack
evaluate reverse polish notation
generate parentheses
daily temperatures
car fleet
largest rectangle in histogram
Common Mistakes
- Popping from an empty stack — always check if the stack is empty before popping
- Forgetting to process remaining elements in the stack after the main loop
- Using a regular stack when a monotonic stack is needed (or vice versa)
- Not storing both value and index when the problem needs positional information
When NOT to Use Stack
- When you need FIFO ordering (use a queue instead)
- When you need to access elements in the middle (use a deque or array)
- When the problem doesn't involve nesting, matching, or nearest greater/smaller elements
Practice Problems
Compare With Other Patterns
Master Stack
Build pattern recognition with interactive MCQs. Understand why to use Stack, not just how.
Download LeetEye Free