Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with a large number.
DSA Java
int[] dp = new int[amount + 1]; for (int i = 1; i <= amount; i++) { dp[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will make the dp array incorrect because 0 means no coins needed.
Using -1 can cause errors in calculations.
Using amount is not large enough to represent unreachable.
✗ Incorrect
We use Integer.MAX_VALUE to represent an initially unreachable state for each amount.
2fill in blank
mediumComplete the code to set the base case for amount 0.
DSA Java
dp[0] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0] to -1 or 1 causes wrong results.
Using amount is incorrect for base case.
✗ Incorrect
No coins are needed to make amount 0, so dp[0] = 0.
3fill in blank
hardFix the error in the condition to check if the coin can be used.
DSA Java
for (int coin : coins) { if (coin [1] i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= will skip valid coins.
Using < excludes coins equal to the amount.
✗ Incorrect
We check if the coin value is less than or equal to the current amount i to use it.
4fill in blank
hardFill both blanks to return the result or -1 if no solution.
DSA Java
return dp[amount] == [1] ? [2] : dp[amount];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 for no solution.
Comparing dp[amount] to 0 instead of Integer.MAX_VALUE.
✗ Incorrect
If dp[amount] is still Integer.MAX_VALUE, it means no solution was found, so return -1.
5fill in blank
hardFill all three blanks to complete the main loop updating dp array.
DSA Java
for (int i = [1]; i <= amount; i++) { for (int coin : coins) { if (coin [2] i && dp[i - coin] != [3]) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 causes redundant checks.
Using < instead of <= excludes valid coins.
Not checking dp[i - coin] != Integer.MAX_VALUE causes errors.
✗ Incorrect
Start from 1 to amount, check coin <= i, and ensure dp[i - coin] is reachable (not Integer.MAX_VALUE).
