Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Consider the recursive function def f(n): if n < 2: return n; return f(n-1) + f(n-2). Evaluate f(6) and count every invocation of f, including the first call. How many calls to f are made in total?

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

Correct answer: D. 25

Each call spawns the recursive branches listed, so f(6) makes 25 invocations in total. The returned value 8 and a smaller argument's call count are different quantities.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic