Debugging recursive programs
Recursive bugs cluster into three buckets: no base case (infinite recursion), wrong combine step (finite but wrong answer), and non-shrinking arguments (you recurse but do not move toward a base). The fastest classroom technique is to trace tiny inputs on paper or in a debugger: arguments should move monotonically toward the base in a dimension you understand (depth, index, numeric size).
logging or targeted print inside a hot recursive function can explode output volume—each frame prints, and exponential algorithms multiply prints further. Prefer unit tests on micro-examples, breakpoints on base-case branches, or a temporary depth counter with a cap.
For data pipelines, reproduce failures on minimal nested payloads: one bad branch in a million-row tree is enough to break aggregation; shrink the JSON until the bug is obvious.
logging how-to: [1].
Sources
Tasks
Card Info
- Topic: Recursion & structural decomposition
- Difficulty: Beginner
- Completed: 0 users