← All patterns
7 / 32
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

  1. Matching / nesting (brackets, tags)?
  2. Need the most-recent unmatched thing?
  3. 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.

Watch it run

Common pitfalls

Popping an empty stack.

Forgetting the stack must be empty at the end.