GATE Computer ScienceHardGATE CS (PYQ pattern)

int fib(int n) { if (n < 2) return n; return fib(n-1) + fib(n-2); } Including the initial call, how many times is fib executed in total while evaluating fib(10)?

  1. A. 89
  2. B. 109
  3. C. 177
  4. D. 143

Correct answer: C. 177

Let C(n) be the number of calls. C(0) = C(1) = 1 and C(n) = 1 + C(n−1) + C(n−2), which equals 2·Fib(n+1) − 1. For n = 10 that is 2 · 89 − 1 = 177.

More Programming and Data Structures (GATE) questions

Exams that ask this topic