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)?
- A. 89
- B. 109
- C. 177
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A hash table of size 13 uses double hashing with h1(k) = k mod 13, h2(k) = 1 + (k mod 11) and probe sequence (h1 + i·h2) mod 13. The keys…
- A dynamic array starts with capacity 1. Whenever an append finds it full, a new array of double the capacity is allocated and all current…
- In an AVL tree a single node has height 0. What is the minimum number of nodes in an AVL tree of height 6?
- int f(int n) { static int c = 0; c += n; if (n > 0) f(n - 1); return c; } int main() { int x = f(4); int y = f(2); printf("%d", x + y); }…
- Assume a 32-bit unsigned int. What does the following program print? #include <stdio.h> int main() { unsigned s = 0; for (unsigned i = 1…
- Assume uint16t is a 16-bit unsigned type, int is 32 bits and the characters are ASCII. What is printed? uint16t h = 7; for (const char p =…