Q&A-3Explain the tree as a recursive definition and how it changes a production decision.
Answer
The definition
A tree is either empty, or a node plus a finite ordered list of trees. That is the whole definition, and it is also the algorithm: base case = empty, recurse = each child, combine = the node's own contribution with the children's results.
def size(node):
if node is None: # base case
return 0
return 1 + size(node.left) + size(node.right) # recurse + combineThe production decision it forces
The definition says nothing about where the recursion lives. In code it lives on the call stack, and the call stack is a small, fixed, per-thread resource. So the decision is: does the traversal run on the call stack, or on an explicit heap stack you control? That decision is set by the depth of the tree, and for anything parsing client input, the depth is chosen by the attacker.
Concrete case: a service that parses nested JSON from clients. A recursive-descent parser on a 100,000-deep [[[[...]]]] payload does not return an error you can map to a 400. It exhausts the stack.
Numbers, CPython 3.12:
sys.getrecursionlimit()defaults to 1000. A recursive walk of a ~1000-node chain raisesRecursionError.- Raising the limit does not buy safety. Each Python call also consumes a C stack frame, and the OS thread stack is 8 MB by default on Linux (
ulimit -s). Past some tens of thousands of frames you get a segfault, not an exception: the process dies with no 500 and no log line.
Iterative version:
def depth_iter(root):
if root is None:
return 0
stack, best = [(root, 1)], 0
while stack:
node, d = stack.pop()
best = max(best, d)
if node.left: stack.append((node.left, d + 1))
if node.right: stack.append((node.right, d + 1))
return bestTrace on a 1000-node right-leaning chain: the recursive version makes 1000 nested calls and dies at the limit. The iterative version pops one tuple and pushes one child, so the stack never holds more than 1 entry: it returns 1000. On a balanced tree of n nodes the explicit stack peaks at O(height) = O(log n) entries.
| Recursive | Explicit stack | |
|---|---|---|
| Depth lives on | call stack, fixed per thread | heap |
| Limit | sys.getrecursionlimit() = 1000 (CPython) | available memory; you set the cap |
| Failure | RecursionError / StackOverflowError (JVM) / RangeError (V8) | MemoryError / OOM kill |
| Per-node cost | one frame | one tuple allocation per push |
| Code | mirrors the definition | extra bookkeeping |
When recursion is still the right call
Bounded, trusted trees: a config AST, a compiler IR, a DOM you built yourself with a known depth cap. Depth ≤ 100, recursion is clearer and cheaper per node because it allocates no tuple per push. The decision flips on one question: can the depth be set by someone who is not you?
Failure modes and detection
RecursionError/StackOverflowError/RangeErrorin logs with a 500s spike → depth exceeded the limit. Alert on the exception name.- Segfault or OOM-kill with no application log → you raised the recursion limit, or the iterative version has no cap. This is the one that pages you at 3am.
- Detection: a load test with a 10,000-deep payload, plus a hard depth cap at the parse boundary, reject at 512 with a 400, so downstream traversals never see a deep tree.
That cap is the real production decision: bound depth once, at the boundary, instead of defending every traversal.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.