Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Recursion proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Define the result of one call, a complete base case and a decreasing argument. Follow descent separately from return, combine independent child results, and choose an explicit stack when Python call depth is unsafe.
1 practice exercise6 interview problems
Recognize it when The input is recursively nested or each instance naturally splits into smaller instances.
Checking your account…
A recursive function solves the current problem by trusting the same function on a strictly smaller input. Define what one call returns without mentally expanding the entire call tree.
The base case must produce a complete answer without another call. It also identifies the decreasing measure—length, remaining depth, interval size, or unvisited nodes—that proves termination.
def countdown_trace(n):
events = []
def visit(value):
events.append(["call", value])
if value > 0:
visit(value - 1)
events.append(["return", value])
visit(n)
return eventsImplement countdown_trace(n). Return events ["call", n] while descending to zero, then ["return", n] while unwinding. n must be a nonnegative integer.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def countdown_trace(n):
events = []
def visit(value):
events.append(["call", value])
if value > 0:
visit(value - 1)
events.append(["return", value])
visit(n)
return eventsPredict the event order from the existing countdown Lab for 1. A call stack remembers where to resume; the return events reverse the descent order even though the code has only one recursive call site.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Parent call | events.append(["call", value]) |
events=[["call", 1]] |
The parent starts but cannot yet emit its return. |
| Smaller child | visit(value - 1) |
value=0 in a new frame |
The decreasing argument reaches a terminal state. |
| Base return | events.append(["return", value]) |
last event=["return", 0] |
The child completes without another call. |
| Resume parent | events.append(["return", value]) |
last event=["return", 1] |
Execution resumes after the suspended child call, with parent value still 1. |
| Output | |
|---|---|
print(countdown_trace(1)) |
[['call', 1], ['call', 0], ['return', 0], ['return', 1]] |
For nested_sum, trust each child return instead of expanding every nested call mentally. Count total entries and maximum nesting separately: they determine work and stack space, respectively.
Work before the recursive call happens while descending; work after it happens while the call stack unwinds. Preorder and postorder behavior differ only in this placement, but their outputs can be completely different.
def nested_sum(value):
if isinstance(value, int):
return value
return sum(nested_sum(item) for item in value)Implement nested_sum(value). value is an integer or a nested list of values. Return the sum of all integers. Nesting must be finite and acyclic.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def nested_sum(value):
if isinstance(value, int):
return value
return sum(nested_sum(item) for item in value)Use recursion when the data or proof is naturally recursive and depth is safely bounded. Use iteration for a simple linear state transition or potentially deep input. Python has finite call-stack depth and does not optimize tail calls.
Say: “The call contract is this, the base case handles this smallest input, and every recursive argument decreases this measure.” Then combine returned values and account for both total calls and O(depth) call-stack space.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Recursion proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Countdown trace with n >= 0 | O(n + 1) | O(n + 1) | One descent and one return event per frame. |
| Nested sum | O(N) | O(N) | N counts all visited integer and list entries in an acyclic nested input. |
O(depth) frames plus returned trace storage when present.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Calling visit(value) again for positive value never approaches the base case. The direct state transition shows no progress.
Prevent it: Reduce the same measure tested by the base case.
next_value = valuenext_value = value - 1Where you will hit this: Partition Traversal Ranges(opens in a new tab)
Computing a child sum and ignoring it leaves the parent total unchanged. Calling the helper is not the same as using its answer.
Prevent it: Add the returned child result to the parent aggregate.
child_sum = sum(child)total += sum(child)Where you will hit this: Partition Traversal Ranges(opens in a new tab)
A nested integer can be zero. if value treats that valid base value as if it were a container, causing iteration over an int.
Prevent it: Check the type or structural base condition rather than truthiness.
result = value if value else sum(value)result = value if isinstance(value, int) else sum(value)Where you will hit this: Build Tree from Inorder and Postorder(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
python-docs · checked 2026-07-27