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[n + 1]; for (int i = 0; i <= n; i++) { dp[i] = [1]; } dp[0] = 0;
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with 0 causes incorrect minimum calculations.
Using negative numbers leads to wrong results.
✗ Incorrect
We initialize dp array with INT_MAX to represent that initially, no amount can be formed except 0.
2fill in blank
mediumComplete the code to iterate over all coin denominations.
DSA C++
for (int i = 1; i <= n; i++) { for (int j = 0; j < [1]; j++) { if (coins[j] <= i) { dp[i] = std::min(dp[i], dp[i - coins[j]] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of m causes out-of-bound errors.
Using dp[i] as loop limit is incorrect.
✗ Incorrect
We loop over all m coin denominations to check which coins can contribute to amount i.
3fill in blank
hardFix the error in the condition to update dp[i].
DSA C++
if (coins[j] <= i && dp[i - coins[j]] != [1]) { dp[i] = std::min(dp[i], dp[i - coins[j]] + 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking dp[i - coins[j]] leads to overflow or wrong results.
Checking against 0 or -1 is incorrect here.
✗ Incorrect
We check dp[i - coins[j]] != INT_MAX to ensure the previous amount is reachable before adding a coin.
4fill in blank
hardFill both blanks to return the minimum coins or -1 if not possible.
DSA C++
return (dp[n] == [1]) ? [2] : dp[n];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 for no solution.
Not checking dp[n] leads to wrong output.
✗ Incorrect
If dp[n] is INT_MAX, amount n cannot be formed, so return -1; otherwise, return dp[n].
5fill in blank
hardFill all three blanks to complete the function signature and initialization.
DSA C++
int coinChange(std::vector<int>& coins, int [1]) { int [2] = [1]; std::vector<int> dp(n + 1, [3]); dp[0] = 0;
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name causes confusion.
Initializing dp with 0 causes incorrect results.
✗ Incorrect
Function takes amount as parameter, uses n as amount variable, and initializes dp with INT_MAX.
