The Stack pattern is based on the Last-In-First-Out (LIFO) principle and is used to process elements in a controlled order, often when the most recent element affects the next decision. Stack-based problems typically involve matching, reversing, validating, or maintaining a monotonic order, making stacks ideal for parsing expressions, handling nested structures, and solving “previous/next greater” type problems.
- When the problem involves nested structures (parentheses, brackets, scopes).
- When you need to undo or rollback previous operations.
- When tracking previous or next greater/smaller elements.
- When order matters and recent elements influence current logic.
pushandpopelements based on conditions.- Using a monotonic stack (increasing or decreasing).
- Storing indices instead of values to compute distances or ranges.
- Simulating recursion or state using an explicit stack.
| # | Name | Java | Python | Go | Link | Company |
|---|---|---|---|---|---|---|
| 1 | Next Greater Element I | ✅ | ❌ | ❌ | LeetCode #496 | |
| 2 | Valid Parentheses | ✅ | ❌ | ❌ | LeetCode #20 | |
| 3 | Implement Queue using Stacks | ✅ | ❌ | ❌ | LeetCode #232 | #tagA |