Data Structures & AlgorithmsVery hardGATE CS · competitive programming
Naive recursion fib(n) = fib(n−1) + fib(n−2), with base cases n < 2, makes calls to itself. How many calls in total does fib(10) make, counting the first call?
- A. 176
- B. 177
- C. 89
- D. 55
Correct answer: B. 177
With C(0) = C(1) = 1 and C(n) = 1 + C(n−1) + C(n−2), C(n) = 2·F(n+1) − 1 = 2·89 − 1 = 177. Forgetting the root call gives 176.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- rec(n) returns at once if n ≤ 1; otherwise it calls rec(n/2) twice with integer division. How many calls are made for n = 16, counting the…
- A singly linked list has a tail of 4 nodes leading into a cycle of 6 nodes. Floyd's method moves a slow pointer 1 step and a fast pointer…
- Reverse each complete group of k = 3 nodes in the list 1 → 2 → … → 10, leaving a final incomplete group as it is. What value is at…
- What is the area of the largest rectangle in the histogram with bar heights [2, 4, 2, 1, 6, 5, 5]?
- In the circular array [1, 5, 3, 4, 2], what is the next greater element of the value 2 at index 4, when the search wraps past the end?
- A window of length 3 slides over [1, 3, −1, −3, 5, 3, 6, 7]. What is the sum of the maximum of each window?