Skip to content
Official PYQ · 1991

GATE CS 1991 Q01 — Recursion

Explain with AI

Consider the following recursive definition of fibfib:

 fib(n) :=  if n = 0 then 1
            else if n = 1 then 1
            else fib(n-1) + fib(n-2)

The number of times fibfib  is called (including the first call) for evaluation of fib(7)fib(7) is___________.

View worked solution

Answer: 41

Let C(n)C(n) denote the total number of times the function fib is called to evaluate fib(n)fib(n).

Based on the recursive definition:

  • For the base cases n=0n = 0 and n=1n = 1, the function returns immediately without any further recursive calls:
    C(0)=1C(0) = 1
    C(1)=1C(1) = 1
  • For n≥2n \ge 2, evaluating fib(n)fib(n) requires 11 initial call plus the calls made during the evaluation of fib(n−1)fib(n-1) and fib(n−2)fib(n-2):
    C(n)=1+C(n−1)+C(n−2)C(n) = 1 + C(n-1) + C(n-2)

We can compute the values of C(n)C(n) step-by-step:

  • C(2)=1+C(1)+C(0)=1+1+1=3C(2) = 1 + C(1) + C(0) = 1 + 1 + 1 = 3
  • C(3)=1+C(2)+C(1)=1+3+1=5C(3) = 1 + C(2) + C(1) = 1 + 3 + 1 = 5
  • C(4)=1+C(3)+C(2)=1+5+3=9C(4) = 1 + C(3) + C(2) = 1 + 5 + 3 = 9
  • C(5)=1+C(4)+C(3)=1+9+5=15C(5) = 1 + C(4) + C(3) = 1 + 9 + 5 = 15
  • C(6)=1+C(5)+C(4)=1+15+9=25C(6) = 1 + C(5) + C(4) = 1 + 15 + 9 = 25
  • C(7)=1+C(6)+C(5)=1+25+15=41C(7) = 1 + C(6) + C(5) = 1 + 25 + 15 = 41

Thus, the total number of times fib is called to evaluate fib(7)fib(7) is 41.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Recursion questions