Data Structures & AlgorithmsHardGATE CS · ICPC regional

The naive recursion fib(n) = fib(n − 1) + fib(n − 2), with base cases fib(0) = 0 and fib(1) = 1, is started by calling fib(6). Counting the very first call, how many calls to fib happen in total?

  1. A. 15
  2. B. 8
  3. C. 25
  4. D. 13

Correct answer: C. 25

Let C(n) be the number of calls: C(n) = 1 + C(n − 1) + C(n − 2) with C(0) = C(1) = 1, which gives C(n) = 2·F(n + 1) − 1. The number of calls is not the Fibonacci value itself.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic