Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with Infinity.
DSA Javascript
const dp = new Array(amount + 1).fill([1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of Infinity causes incorrect minimum calculations.
Using null or -1 does not work for numeric comparisons.
✗ Incorrect
We use Infinity to represent that initially, the minimum coins needed for each amount is unknown and very large.
2fill in blank
mediumComplete the code to set the base case for amount 0.
DSA Javascript
dp[0] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0] to Infinity or -1 causes wrong results.
Forgetting to set dp[0] causes errors in the loop.
✗ Incorrect
For amount 0, zero coins are needed, so dp[0] = 0.
3fill in blank
hardFix the error in the inner loop condition to check if coin can be used.
DSA Javascript
for (let coin of coins) { if (coin [1] i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= causes skipping valid coins.
Using < excludes the case when coin equals i.
✗ Incorrect
We check if coin is less than or equal to i to ensure we can use this coin for amount i.
4fill in blank
hardFill both blanks to complete the function signature and return statement.
DSA Javascript
function [1](coins, amount) { // ...code... return dp[[2]] === Infinity ? -1 : dp[amount]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function name causes confusion.
Checking dp[coins] or dp.length is incorrect.
✗ Incorrect
The function is named coinChange and we check dp[amount] to decide the return value.
5fill in blank
hardFill all three blanks to complete the main loop and update dp array.
DSA Javascript
for (let i = 1; i <= [1]; i++) { for (let coin of [2]) { if (coin [3] i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limits causes incorrect dp updates.
Using wrong variable names causes errors.
✗ Incorrect
We loop i from 1 to amount, iterate coins, and check if coin <= i before updating dp[i].
