synopsis · Thinking tools · chapter 6
Mindset & Problem Solving
9 cards, about a minute. Read them the night before an interview, or right after the chapter to lock it in.
- what it is
A routine, not genius: Understand → Match → Plan → Implement → Review → Evaluate. Same steps every time, so you always know the next one.
- use it when
Every coding round and every practice problem — and the moment you freeze. Next step is always: restate it, then solve one tiny example by hand.
- template
Say it out loud: "Brute force is O(n²) because… The repeated work is… A
HashMapmakes that O(1), so O(n) time, O(n) space." - cost
Constraints pick the idea: n ≤ 20 → 2ⁿ · n ≤ ~5,000 → n² (10⁴ borderline) · n ≤ 10⁵–10⁶ → n log n or n. Budget ≈ 10⁸ simple steps per second.
- trap
Coding before understanding. Most wrong answers solve a slightly different problem. Restate it and run one example by hand first.
- trap
intstops at 2,147,483,647. Products like 10⁵ × 10⁵ or sums of many big values needlong— cast before the operation. - pro tip
Dry run with a table (one row per iteration, one column per variable), then test: empty, one element, duplicates, negatives, max values.
- pro tip
Practice: stuck 20 minutes → read one hint. Re-solve from a blank page after 1, 3, 7, 16 and 35 days.
- pro tip
At home, stress test: compare your fast code with the brute force on thousands of tiny random inputs (fixed seed).
Shaky on a card? Open the chapter or flip its recall cards.