Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with a large number.
DSA C#
int[] dp = new int[amount + 1]; for (int i = 1; i <= amount; i++) { dp[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of a large number causes incorrect minimum calculations.
Using negative numbers can cause logic errors.
✗ Incorrect
We initialize dp[i] with int.MaxValue to represent that the amount i is initially unreachable.
2fill in blank
mediumComplete the code to update dp[i] with the minimum coins needed.
DSA C#
for (int i = 1; i <= amount; i++) { foreach (int coin in coins) { if (i >= coin && dp[i - coin] != int.MaxValue) { dp[i] = Math.[1](dp[i], dp[i - coin] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.Max will give the wrong result.
Using Math.Abs or Math.Round is not relevant here.
✗ Incorrect
We use Math.Min to keep the smallest number of coins needed for amount i.
3fill in blank
hardFix the error in the return statement to handle unreachable amounts.
DSA C#
return dp[amount] == [1] ? -1 : dp[amount];
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 0 or -1 causes incorrect results.
Returning dp[amount] without check may return a large number.
✗ Incorrect
If dp[amount] is int.MaxValue, it means the amount cannot be formed, so return -1.
4fill in blank
hardFill both blanks to complete the loop that iterates over amounts and coins.
DSA C#
for (int [1] = 1; [1] <= [2]; [1]++) { foreach (int coin in coins) { if ([1] >= coin && dp[[1] - coin] != int.MaxValue) { dp[[1]] = Math.Min(dp[[1]], dp[[1] - coin] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using j as loop variable is confusing here.
Using coins.Length as loop limit is incorrect.
✗ Incorrect
The outer loop variable is i, iterating from 1 to amount.
5fill in blank
hardFill all three blanks to complete the code that populates the dictionary mapping coins to their counts if count is positive.
DSA C#
var result = new Dictionary<int, int>(); for (int [3] = 0; [3] < coins.Length; [3]++) { if (counts[[3]] > 0) { result[[1]] = [2]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like coin instead of i.
Mixing up keys and values in the dictionary.
✗ Incorrect
We map each coin (coins[i]) to its count (counts[i]) using index i.
