Data Structures & AlgorithmsVery hardICPC regional · GATE CS top band
Consider the recursive function def f(n): if n < 2: return n; return f(n-1) + f(n-2). Evaluate f(10) and count every invocation of f, including the first call. How many calls to f are made in total?
- A. 218
- B. 55
- C. 109
- D. 177
Correct answer: D. 177
Each call spawns the recursive branches listed, so f(10) makes 177 invocations in total. The returned value 55 and a smaller argument's call count are different quantities.
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
- Array [25, 3, 24, 17, 29, 1] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element at…
- Array A = [8, 27, 25, 23, 28, 20, 13] (all values below 32). What is the maximum of A[i] XOR A[j] over all index pairs with 0 ≤ i < j < 7?
- Undirected weighted graph on vertices A, B, C, D, E; its edges, in this listed order, are: C–E (9), A–C (8), A–B (5), B–E (3), D–E (2)…
- Union–find on elements 0 to 7, all starting as singletons, with no path compression. union(x, y): let rx, ry be the roots of x and y; if…
- 2 × 3 sliding puzzle with tiles 1 to 5 and one blank (shown as ). Current board: 4 1 3 / 2 5 . Goal board: 1 2 3 / 4 5 . A move slides a…
- Directed graph on vertices A, B, C, D, E, F with edges D→E, A→B, C→D, F→E, F→C, E→D, A→D, C→A. How many strongly connected components does…