Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern

A recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(7)?

  1. A. 86
  2. B. 43
  3. C. 61
  4. D. 85

Correct answer: D. 85

Building up the memo table from f(0) = f(1) = 1 using f(n) = f(n−1) + 2·f(n−2) step by step gives f(7) = 85. 43 is actually f(6), a common off-by-one when the table is read one step too early.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic