Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with a large number.
DSA Go
dp := make([]int, amount+1) for i := 1; i <= amount; i++ { dp[i] = [1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp[i] with 0 causes incorrect minimum calculations.
Using -1 can cause logic errors in comparisons.
✗ Incorrect
We initialize dp[i] with amount+1 as a large number representing infinity, since the maximum coins needed can't exceed amount.
2fill in blank
mediumComplete the code to update dp[i] with the minimum coins needed.
DSA Go
for _, coin := range coins { if coin <= i { dp[i] = [1](dp[i], dp[i-coin]+1) } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using max instead of min leads to wrong results.
Forgetting to add 1 for the current coin.
✗ Incorrect
We use min to keep the smallest number of coins needed for amount i.
3fill in blank
hardFix the error in the return statement to handle no solution case.
DSA Go
if dp[amount] == [1] { return -1 } return dp[amount]
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 instead of amount+1.
Returning dp[amount] without checking leads to wrong answers.
✗ Incorrect
If dp[amount] is still amount+1, it means no combination was found, so return -1.
4fill in blank
hardFill both blanks to complete the loop that fills dp array.
DSA Go
for i := [1]; i <= amount; i++ { for _, coin := range [2] { if coin <= i { dp[i] = min(dp[i], dp[i-coin]+1) } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting i from 0 causes unnecessary checks.
Looping over amount instead of coins causes errors.
✗ Incorrect
We start i from 1 to amount and iterate over coins to update dp.
5fill in blank
hardFill all three blanks to define the min function and call it correctly.
DSA Go
func min(a, b int) int { if [1] < [2] { return [3] } return b }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning b when a is smaller.
Swapping parameters in comparison.
✗ Incorrect
The min function compares a and b and returns the smaller one.
