Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array for coin change.
DSA Java
int[] dp = new int[[1] + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using coins.length instead of amount for dp size.
Forgetting to add 1 to amount for dp array size.
✗ Incorrect
The dp array size should be amount + 1 to represent all amounts from 0 to amount.
2fill in blank
mediumComplete the code to set the base case for dp array.
DSA Java
dp[[1]] = 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[1] = 1 instead of dp[0].
Setting dp[amount] = 1 incorrectly.
✗ Incorrect
There is exactly one way to make amount 0: use no coins.
3fill in blank
hardFix the error in the inner loop condition to avoid index out of bounds.
DSA Java
for (int j = [1]; j <= amount; j++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Starting j from 0 causes negative index access.
Starting j from 1 causes errors if coin value > 1.
✗ Incorrect
The inner loop starts from coins[i] to ensure valid indices when accessing dp[j - coins[i]].
4fill in blank
hardFill both blanks to update dp array correctly inside nested loops.
DSA Java
dp[j] = dp[j] [1] dp[j [2] coins[i]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition.
Using addition instead of subtraction inside index.
✗ Incorrect
We add the ways to make amount j without current coin and ways to make amount j - coin value.
5fill in blank
hardFill all three blanks to complete the coin change function returning total ways.
DSA Java
public int change(int [1], int[] [2]) { int[] dp = new int[[3] + 1]; dp[0] = 1; for (int i = 0; i < coins.length; i++) { for (int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names.
Incorrect dp array size.
✗ Incorrect
The function parameters are amount and coins array; dp array size is amount + 1.
