Data Structures & AlgorithmsHardProduct-company MCQ pattern

How many times in total is fib called (including the first call) when fib(6) is evaluated?

How many times in total is fib called (including the first call) when fib(6) is evaluated?
int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
  1. A. 25
  2. B. 13
  3. C. 15
  4. D. 7

Correct answer: A. 25

Calls C(n) = 1 + C(n − 1) + C(n − 2) with C(0) = C(1) = 1 give 1, 1, 3, 5, 9, 15, 25 for n = 0..6, so 25. 13 is fib(7), and 7 is the number of distinct subproblems a memoised version would solve.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic