Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many ways are there to make 15 from unlimited coins of value 1, 5 and 10 (order does not matter)? Run the DP.
Problem: How many ways are there to make 15 from unlimited coins of value 1, 5 and 10 (order does not matter)? Run the DP.
ways = [1] + [0] * 15
for c in (1, 5, 10):
for v in range(c, 16):
ways[v] += ways[v - c]
print(ways[15])What is printed?- A. 6
- B. 42
- C. 2
- D. 4
Correct answer: A. 6
Processing one coin type at a time counts multisets: 15 = 10+5, 10+1×5, 5×3, 5×2+1×5, 5+1×10, 1×15, so 6. Looping amounts outside coins counts ordered sequences (42), dropping the 1-coin gives 2, and 4 is the count for 10.
More Coding Interview Problems questions
- Problem: How many integers in [1, 100000] are multiples of 7 whose last digit is 3? Run the code. python print(sum(1 for x in range(1…
- Problem: How many ordered pairs (x, y) with 1 <= x, y <= 12 satisfy x^2 + y^2 <= 100? Run the code. python print(sum(1 for x in range(1…
- Problem: How many lattice points (x, y) with x >= 0, y >= 0 and x + y <= 10 lie in the triangle? Run the code. python print(sum(1 for x in…
- Problem: What is the sum of the ASCII codes of the characters of 'hello'? Run the code. python print(sum(ord(c) for c in 'hello')) What is…
- Problem: What is the length of the longest strictly increasing contiguous subarray of [3, 4, 5, 1, 2, 3, 4, 5, 6]? Run the code. python a…
- Problem: What is the minimum number of cuts needed to partition 'abcde' into palindromes? Run the DP. python s = 'abcde' n = len(s) pal =…