Stacks, Queues & Heaps
Stack
When you see it
Signal words
If the prompt uses any of these, this pattern should come to mind first.
matchingvalid parenthesesnestedundolast seenbalanced
Practice
Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.
Ask yourself
- Matching / nesting (brackets, tags)?
- Need the most-recent unmatched thing?
- Evaluate or simplify left-to-right with backtracking of one step?
Classic problem
Valid Parentheses (LeetCode 20) — is a string of ()[]{} correctly matched? Push each
opener; on a closer, the top must be its partner.
Input: s = "()[]{}"
Output: true — every opener is immediately matched and the stack is empty at the end.
Common pitfalls
Popping an empty stack.
Forgetting the stack must be empty at the end.