def mystery(n):
if n <= 0:
return 1
else:
return mystery(n-1) + mystery(n-2)
Now, consider the following function call:
mystery(4)
Assume that a typical runtime stack is used to manage function calls. Each function call is pushed onto the stack and removed only after it finishes execution.
Which of the following options denotes the total number of function calls (i.e., the total number of stack activations), including the initial call, to compute mystery(4)?
C
Step-by-Step Solution
Insight: The total number of function calls follows a recurrence relation , where the accounts for the current function call itself.
Exam route: Compute iteratively from base cases. . Then , , , .
Learning route: Draw the recursion tree for mystery(4). The root is 1 call. It branches to mystery(3) and mystery(2). Count all nodes in this call tree. Total nodes = 15. Note that mystery(0) and mystery(-1) are base cases that make 1 call each and return immediately without further branching.