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?- A. 6765
- B. 17711
- C. 1048576
- 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
- Problem: How many 4-digit numbers have all digits equal to or greater than the digit before them (non-decreasing)? Run the code. python…
- Problem: What is the maximum value of a[i] XOR a[j] over i < j for a = [3, 10, 5, 25, 2, 8, 17]? Run the code. python a = [3, 10, 5, 25…
- Problem: How many subarrays of [1, -1, 1, -1, 1] have sum 0? Run the prefix-count code. python from collections import Counter a = [1, -1…
- Problem: What is the number of paths from (0,0) to (4,4) using right and up moves that never touch the diagonal cells (i, i) except at the…
- Problem: What is the number of integers in [1, 10000] that are perfect squares and also end in the digit 9? Run the code. python…
- Problem: How many ways are there to split the string '1234' into two non-empty parts whose integer values are both divisible by 3? Run the…