Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard
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(9)?
- A. 341
- B. 342
- C. 163
- D. 171
Correct answer: A. 341
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(9) = 341. 171 is actually f(8), a common off-by-one when the table is read one step too early.
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
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 2, 2, 2, 1, 1, 0] (values 0, 1, 2 only) in…
- What is the postfix (Reverse Polish) form of the infix expression "e (a + b)- h / d", built with the standard stack-based conversion (^…
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(2), enqueue(3), enqueue(3), enqueue(2), enqueue(3)…
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- Keys 26, 63, 80, 39, 14, 99, 18, 93, 44 are inserted via linear probing (h(k) = k mod 11, probe by +1 on collision) into an initially…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(3), enqueue(1), enqueue(6), enqueue(9), dequeue()…