Data Structures & AlgorithmsModerateTCS NQT · DP warm-up
Problem: How many ways can 10 be formed as an ordered-free sum of coins 1, 2 and 5 (each coin may be used any number of times, and order does not matter)?
Problem: How many ways can 10 be formed as an ordered-free sum of coins 1, 2 and 5 (each coin may be used any number of times, and order does not matter)?
ways = [1] + [0] * 10
for c in [1, 2, 5]:
for v in range(c, 11):
ways[v] += ways[v - c]
print(ways[10])What is printed?- A. 6
- B. 12
- C. 10
- D. 8
Correct answer: C. 10
Using 0, 1 or 2 fives gives 6 + 3 + 1 = 10 combinations of the 1s and 2s. Counting ordered sequences instead would give a much larger number.
More Coding Interview Problems questions
- Problem: What is the diameter (in edges) of the tree with edges 1-2, 2-3, 2-4, 4-5, 5-6? Use the double BFS code. python from collections…
- Problem: A robot starts at (0, 0) facing north (+y). Commands: F moves one step forward, B one step backward (facing unchanged), L turns…
- Problem: What is the KMP prefix-function array pi for the string 'ababaca'? python s = 'ababaca' pi = [0] len(s) for i in range(1…
- Problem: What is the lexicographically smallest rotation of 'bcabca'? python s = 'bcabca' print(min(s[i:] + s[:i] for i in range(len(s))))…
- Problem: Items (weight, value) are (2,3), (3,4), (4,5), and each may be used at most once. Capacity W = 7. Run the code below to find the…
- Problem: How many ways can a staircase of 8 steps be climbed if each move climbs 1, 3 or 4 steps? Run the DP below. python ways = [0] 9…