| Bug | Example | Symptom | Fix |
|---|---|---|---|
| no base case | f(n) { return f(n - 1); } | StackOverflowError | always write the base case first |
| no progress | f(n) { ... return f(n); } or f(n + 1) heading away from the base | StackOverflowError | check every call moves toward the base case |
| base case misses inputs | base n == 0, but called with negative n | StackOverflowError on some inputs | use n <= 0 / handle all "smallest" cases |
| ignoring the return value | countNotes(sub); without total += | answer too small | use every recursive result |
| forgetting to unchoose | backtracking without undo | wrong/duplicated results | undo every change after exploring (Ch 6) |
| sharing a mutable result by accident | out.add(chosen) storing the same StringBuilder/list | every result looks identical at the end | store a copy: out.add(chosen.toString()) / new ArrayList<>(path) |
| hidden copying | substring, copyOfRange at every level | O(n²) instead of O(n) | recurse on indexes |
| exponential blow-up | overlapping subproblems unmemoized | TLE on moderate n | memoize (Ch 7) |
| too deep | linear recursion on 10⁵+ elements | StackOverflowError | explicit stack / loop (Ch 8) |
A debugging technique that always works: print the arguments on entry and the return value on exit, with indentation by depth — exactly the trace in Chapter 2. Run it on the smallest input that fails.