Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with a large number.
DSA Python
dp = [[1]] * (amount + 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of a large number causes incorrect minimum calculations.
Using -1 can cause logic errors in comparisons.
✗ Incorrect
We use float('inf') to represent an initially unreachable amount.
2fill in blank
mediumComplete the code to set the base case for amount 0.
DSA Python
dp[0] = [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0] to a large number causes wrong results.
Using 1 instead of 0 adds an extra coin unnecessarily.
✗ Incorrect
0 coins are needed to make amount 0.
3fill in blank
hardFix the error in the inner loop condition to check if coin can be used.
DSA Python
for coin in coins: for x in range(coin, amount + 1): if dp[x - coin] != [1]: dp[x] = min(dp[x], dp[x - coin] + 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 instead of float('inf') causes wrong updates.
Using -1 causes logic errors.
✗ Incorrect
We check if dp[x - coin] is not infinity before using it.
4fill in blank
hardFill both blanks to return the result or -1 if no solution.
DSA Python
return dp[amount] if dp[amount] != [1] else [2]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 for no solution.
Not checking dp[amount] leads to wrong answers.
✗ Incorrect
If dp[amount] is infinity, no solution exists, so return -1.
5fill in blank
hardFill all three blanks to complete the coin change function.
DSA Python
def coin_change(coins, amount): dp = [[1]] * (amount + 1) dp[0] = [2] for coin in coins: for x in range(coin, amount + 1): if dp[x - coin] != [3]: dp[x] = min(dp[x], dp[x - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect initialization of dp array.
Wrong base case value.
Not checking dp[x - coin] properly.
✗ Incorrect
Initialize dp with infinity, set dp[0] to 0, and check dp[x - coin] != infinity.
