Data Structures & AlgorithmsHardTCS CodeVita · Microsoft SDE OA
Coins of denominations 1, 3 and 4 are available with unlimited supply. For the amount 6, run the DP that finds the minimum number of coins summing exactly to 6.
Coins of denominations 1, 3 and 4 are available with unlimited supply. For the amount 6, run the DP that finds the minimum number of coins summing exactly to 6.
coins=[1,3,4]; A=6; INF=10**9
g=[0]+[INF]*A
for x in range(1,A+1):
g[x]=min([g[x-c]+1 for c in coins if c<=x]+[INF])
print(g[A])What is printed?- A. 4
- B. 3
- C. 2
- D. -1
Correct answer: C. 2
Unbounded coin DP gives 3+3, which is 2 coins. Greedy takes 4+1+1 and uses 3 coins, counting combinations instead of coins gives 4, and treating each coin as single-use makes 6 unreachable.
More Coding Interview Problems questions
- Array a = [1, 3, 2, 3, 1, 4, 5, 2]. Subsequences are chosen by index (positions may repeat values). Count the strictly increasing…
- Two strings are "intention" and "execution". Compute the minimum cost to turn the first into the second, where insertion and deletion cost…
- A staircase is climbed with steps of size 1, 3 or 4 (order of steps matters). How many distinct ordered sequences of steps reach exactly…
- Forty-one people stand in a circle numbered 1 to 41. Starting from person 1, every third person is removed, repeatedly, counting continues…
- Compute the sum of the prefix-function (KMP failure) array of the string "abacabab", where pi[i] is the length of the longest proper…
- A directed weighted graph has edges 0→1 (2), 0→2 (2), 1→3 (1), 2→3 (1), 1→4 (4), 2→4 (5), 3→4 (3), 3→5 (4), 4→5 (1). Count the number of…