Recursion vs Iteration and Memory
Problem
A service stores hierarchical data as a tree and computes its depth with the obvious recursive function. In production it crashes on a particular customer's data with a RecursionError, even though the tree has only 50,000 nodes and the machine has gigabytes of memory.
- Explain what memory a recursive call consumes, why the crash depends on the shape of the tree rather than its size, and why the interpreter refuses long before physical memory runs out.
- Rewrite the depth computation iteratively with an explicit stack, so it handles any shape, and show that the recursive and iterative versions agree on ordinary trees.
- State the space complexity of both versions precisely, and describe when recursion is still the right choice.
- Tree nodes have
leftandrightchildren; a "degenerate" tree is a chain where every node has only one child. - Do not raise the recursion limit as the fix. Explain why that is a bandage.
Examples
- Balanced tree of 50,000 nodes: depth about 16, recursion depth 16, both versions run fine.
- Chain of 50,000 nodes: depth 50,000. The recursive version needs 50,000 simultaneous frames; CPython's default limit is 1,000, so it raises
RecursionErrorafter about 1,000 calls. The iterative version pushes at most 50,000 entries onto a list and returns 50,000. - Chain of 5,000 nodes in Python with the limit raised to 10,000: works. Before CPython 3.11 every Python call also consumed C stack, so a 10⁶-deep recursion would overflow the 8 MB C stack and segfault long before the heap ran out. Since 3.11 pure-Python frames live on the heap, so 10⁶ deep merely costs on the order of 100 MB of frame objects, and any C-level call in the chain (a
__lt__insidemax, ajsondecode) still has a separate, smaller C limit.
Hints
- What is stored per call: return address, saved registers, local variables, and in CPython a frame object. Where does it live, and how big is that region?
- Recursion depth equals the length of the longest path being explored, not the number of nodes. What is that for a chain?
- An explicit stack is a list on the heap. What does it store per entry, and what limits its size?
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.