Data Structures & AlgorithmsHardICPC regional · TCS NQT
Problem: How many recursive calls are made in total when naive Fibonacci fib(n) = fib(n−1) + fib(n−2) with fib(0) = fib(1) = base cases, is called as fib(10)? Count every call including the initial call and base cases.
- A. 177
- B. 55
- C. 88
- D. 19
Correct answer: A. 177
Calls satisfy C(n) = 1 + C(n − 1) + C(n − 2) with C(0) = C(1) = 1, so C(n) = 2·F(n + 1) − 1 = 2·89 − 1 = 177. 55 is the value fib(10), 88 counts only the non-base (internal) calls, and a memoised version makes just 19 calls.
More Coding Interview Problems questions
- Problem: What is printed? print(f"{3.14159:.2f}", f"{42:05d}", f"{0.5:.0%}")
- Problem: In Python, what is the value of (−5 >> 1) + 10, where >> is the arithmetic right shift applied to the negative integer −5?
- Problem: Records (name, score) are sorted by score descending with Python's stable sort…
- Problem: A deque holds 1, 2, 3, 4, 5 from left to right. The operation rotate(2) moves the last 2 elements to the front (rotate right by…
- Problem: A 2D prefix-sum table is built for the grid [[1,2,3],[4,5,6],[7,8,9]]. What is the sum of the submatrix with rows 1–2 and columns…
- Problem: A sparse-table-free query: over the array [5, 2, 8, 1, 9, 3, 7], what is the minimum value on the range of indices [2, 5]…