ProgrammingVery hardProduct company OA

How many TOTAL calls to fib (including the initial call) are made evaluating fib(4), given this naive recursive implementation?

How many TOTAL calls to `fib` (including the initial call) are made evaluating `fib(4)`, given this naive recursive implementation?
int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
  1. A. 4
  2. B. 7
  3. C. 5
  4. D. 9

Correct answer: D. 9

Expanding the call tree: fib(4) calls fib(3)+fib(2); fib(3) calls fib(2)+fib(1); each fib(2) calls fib(1)+fib(0). Counting every invocation (fib(4), fib(3), fib(2)x2, fib(1)x3, fib(0)x2) totals 9 calls.

More C Programming questions

Exams that ask this topic