Assume a typical runtime stack is used for the recursive function mystery(n) as defined earlier.
How many total function calls (stack activations), including the initial call, are made to compute mystery(4)?
Correct Answer :
15
Solution :
The correct option is 15.
To understand why computing mystery(4) requires exactly 15 total function calls, we can analyze the standard recursive structure of the function. Typically, a function like mystery(n) that exhibits this behavior is defined as follows:
- Base Case: If , it returns immediately (performing 1 activation and making no further recursive calls).
- Recursive Step: If , it makes two recursive calls to mystery(n - 1).
We can visualize and count the total function calls (stack activations) by constructing a recursion tree for mystery(4):
1. Level 0 (Initial Call):
- mystery(4) is called (1 call).
- Since 4 > 1, it triggers 2 recursive calls: mystery(3).
2. Level 1:
- There are 2 calls of mystery(3).
- Each mystery(3) call triggers 2 recursive calls: mystery(2) (making a total of 4 calls).
3. Level 2:
- There are 4 calls of mystery(2).
- Each mystery(2) call triggers 2 recursive calls: mystery(1) (making a total of 8 calls).
4. Level 3 (Base Case):
- There are 8 calls of mystery(1).
- Since 1 ≤ 1, these calls hit the base case and return immediately without making any further calls.
To find the total number of function calls, we sum the number of activations at each level of the recursion tree:
Thus, a total of 15 stack activations are made to compute mystery(4).
Access expert-curated educational resources and study materials—completely free.
Create, conduct, and manage professional online assessments with Mindyard. Perfect for teachers and institutes.
Copyright © 2026 Mindyard. All Rights Reserved.