Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the number of ways to write 20 as an ordered sum of the parts 1 and 2 (compositions)? Run the DP.

Problem: What is the number of ways to write 20 as an ordered sum of the parts 1 and 2 (compositions)? Run the DP.
f = [1, 1]
for i in range(2, 21):
    f.append(f[-1] + f[-2])
print(f[20])
What is printed?
  1. A. 6765
  2. B. 17711
  3. C. 1048576
  4. D. 10946

Correct answer: D. 10946

f(n) = f(n − 1) + f(n − 2) with f(0) = f(1) = 1, so f(20) = F(21) = 10946. Indexing one off gives 6765 or 17711, and 2²⁰ = 1048576 ignores that the parts must sum to 20.

More Coding Interview Problems questions

Exams that ask this topic