Predict the output
What does this print?
def depth(n: int) -> int:
if n == 0:
return 0
return 1 + depth(n - 1)
print(depth(50))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 50
Right: 50 nested calls are pending at the deepest point.
Fifty frames are alive at once at the bottom of the recursion, which is its O(n) space cost.
Why not the others?
- B 1. Each call adds one before returning, so the total is the depth.
- C 0. The base case returns 0, but each level above it adds 1.