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)Solve a problem by reducing it to smaller instances with explicit base cases. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Recursion when the prompt's constraints and required operations match this shape: Solve a problem by reducing it to smaller instances with explicit base cases.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
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.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
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.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
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 |
|---|---|---|---|
| Recursion complete workflow | O(n) | O(n) | When one traversal identifies the root and another partitions left from right, index ranges avoid copying subarrays at each recursive call. Each frame describes matching node sets, and the inorder root offset determines the exact left-subtree size in preorder. |
O(n) for the focused Partition Traversal Ranges implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: Every recursive call reduces a well-defined measure and all terminal states have explicit base cases.
Avoid
def partition_traversal_ranges(preorder, inorder):
passUse instead
def partition_traversal_ranges(preorder, inorder):
positions = {value: index for index, value in enumerate(inorder)}
frames = []
def partition(pre_left, pre_right, in_left, in_right):
if pre_left >= pre_right:
return
frames.append([pre_left, pre_right, in_left, in_right])
root_index = positions[preorder[pre_left]]
left_size = root_index - in_left
partition(pre_left + 1, pre_left + 1 + left_size, in_left, root_index)
partition(pre_left + 1 + left_size, pre_right, root_index + 1, in_right)
partition(0, len(preorder), 0, len(inorder))
return framesWhere you will hit this: Partition Traversal Ranges(opens in a new tab)
Omits one position from both preorder child boundaries, producing incorrect or nonshrinking recursive frames.
Prevent it: Preserve this proof obligation: The decreasing measure guarantees termination, and each return preserves the contract of its smaller subproblem.
Avoid
def partition_traversal_ranges(preorder, inorder):
positions = {value: index for index, value in enumerate(inorder)}
frames = []
def partition(pl, pr, il, ir):
if pl >= pr: return
frames.append([pl, pr, il, ir])
root = positions[preorder[pl]]
left_size = root - il
partition(pl + 1, pl + left_size, il, root)
partition(pl + left_size, pr, root + 1, ir)
partition(0, len(preorder), 0, len(inorder))
return framesUse instead
def partition_traversal_ranges(preorder, inorder):
positions = {value: index for index, value in enumerate(inorder)}
frames = []
def partition(pre_left, pre_right, in_left, in_right):
if pre_left >= pre_right:
return
frames.append([pre_left, pre_right, in_left, in_right])
root_index = positions[preorder[pre_left]]
left_size = root_index - in_left
partition(pre_left + 1, pre_left + 1 + left_size, in_left, root_index)
partition(pre_left + 1 + left_size, pre_right, root_index + 1, in_right)
partition(0, len(preorder), 0, len(inorder))
return framesWhere you will hit this: Partition Traversal Ranges(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(n).
Avoid
def partition_traversal_ranges(preorder, inorder):
positions = {value: index for index, value in enumerate(inorder)}
frames = []
def partition(pl, pr, il, ir):
if pl >= pr: return
frames.append([pl, pr, il, ir])
root = positions[preorder[pl]]
left_size = root - il
partition(pl + 1, pl + left_size, il, root)
partition(pl + left_size, pr, root + 1, ir)
partition(0, len(preorder), 0, len(inorder))
return framesUse instead
def partition_traversal_ranges(preorder, inorder):
positions = {value: index for index, value in enumerate(inorder)}
frames = []
def partition(pre_left, pre_right, in_left, in_right):
if pre_left >= pre_right:
return
frames.append([pre_left, pre_right, in_left, in_right])
root_index = positions[preorder[pre_left]]
left_size = root_index - in_left
partition(pre_left + 1, pre_left + 1 + left_size, in_left, root_index)
partition(pre_left + 1 + left_size, pre_right, root_index + 1, in_right)
partition(0, len(preorder), 0, len(inorder))
return framesWhere 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