Data Structures & AlgorithmsHardProduct-company MCQ pattern
How many times in total is fib called (including the first call) when fib(6) is evaluated?
How many times in total is fib called (including the first call) when fib(6) is evaluated?
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}- A. 25
- B. 13
- C. 15
- D. 7
Correct answer: A. 25
Calls C(n) = 1 + C(n − 1) + C(n − 2) with C(0) = C(1) = 1 give 1, 1, 3, 5, 9, 15, 25 for n = 0..6, so 25. 13 is fib(7), and 7 is the number of distinct subproblems a memoised version would solve.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What does fun(3) print? c void fun(int n) { if (n > 0) { fun(n - 1); printf("%d ", n); fun(n - 1); } }
- What are the time complexities of versions P and Q? c int P(int n) { if (n <= 1) return 1; return P(n/2) + P(n/2) + n; } int Q(int n) { if…
- What is the time complexity of solve(n)? python def solve(n): if n <= 0: return for i in range(n): pass # O(1) work solve(n - 1)
- How many of these statements are correct? (i) Dijkstra's algorithm with a binary heap runs in O((V + E) log V). (ii) Bellman–Ford runs in…
- Directed edges: A→B (2), A→C (5), C→B (−4), B→D (1). Dijkstra's algorithm, which finalises each vertex once when it is removed from the…
- DFS is run on a directed graph with edges A→B, A→C, B→D, C→D, C→A, D→B, E→C, E→D, starting from A, taking vertices and neighbours in…