Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the minimum coins array with a large number.
DSA C
int minCoins[amount + 1]; for (int i = 0; i <= amount; i++) { minCoins[i] = [1]; } minCoins[0] = 0;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will make all values zero and break the logic.
Using -1 is invalid for minimum calculation.
✗ Incorrect
We initialize minCoins with amount + 1 as a large number to represent infinity since the maximum coins needed can't exceed amount.
2fill in blank
mediumComplete the code to loop through each coin for the current amount.
DSA C
for (int i = 1; i <= amount; i++) { for (int j = 0; j < [1]; j++) { if (coins[j] <= i) { // update minCoins[i] } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using amount instead of numCoins causes out-of-bound errors.
Using coins as loop limit is invalid since coins is an array.
✗ Incorrect
We loop through all coins using numCoins which is the count of coins available.
3fill in blank
hardFix the error in updating minCoins[i] to find the minimum coins needed.
DSA C
if (coins[j] <= i) { minCoins[i] = (minCoins[i] > minCoins[i - coins[j]] + 1) ? [1] : minCoins[i]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 to minCoins[i] instead of minCoins[i - coins[j]] causes wrong results.
Using coins[j] directly is incorrect.
✗ Incorrect
We update minCoins[i] with minCoins[i - coins[j]] + 1 because we add one coin to the minimum coins needed for the smaller amount.
4fill in blank
hardFill both blanks to return the correct result or -1 if no solution exists.
DSA C
return (minCoins[amount] == [1]) ? [2] : minCoins[amount];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 when no solution exists.
Comparing with INT_MAX instead of amount + 1.
✗ Incorrect
If minCoins[amount] is still amount + 1, it means no solution was found, so return -1.
5fill in blank
hardFill all three blanks to complete the function signature and variable declarations.
DSA C
int [1](int* coins, int [2], int [3]) { int minCoins[amount + 1]; // function body }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent parameter names.
Using coinsCount instead of numCoins.
✗ Incorrect
The function is named coinChange, takes coins array, number of coins as numCoins, and amount as amount.