Challenge - 5 Problems
Coin Change Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of minimum coins for amount 11
What is the output of the following code that calculates the minimum number of coins needed to make amount 11 using coins [1, 2, 5]?
DSA Python
def min_coins(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for i in range(1, amount + 1): for coin in coins: if coin <= i: dp[i] = min(dp[i], dp[i - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1 print(min_coins([1, 2, 5], 11))
Attempts:
2 left
💡 Hint
Think about how many coins of 5, 2, and 1 can sum to 11 with minimum count.
✗ Incorrect
The minimum coins to make 11 are 5 + 5 + 1, which is 3 coins.
❓ Predict Output
intermediateMinimum coins for amount 7 with coins [2, 4]
What is the output of the code below for amount 7 using coins [2, 4]?
DSA Python
def min_coins(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for i in range(1, amount + 1): for coin in coins: if coin <= i: dp[i] = min(dp[i], dp[i - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1 print(min_coins([2, 4], 7))
Attempts:
2 left
💡 Hint
Check if 7 can be formed by any combination of 2 and 4.
✗ Incorrect
7 cannot be formed by any combination of 2 and 4, so the output is -1.
🔧 Debug
advancedIdentify the error in coin change code
What error does the following code produce when run?
DSA Python
def min_coins(coins, amount): dp = [0] * (amount + 1) for i in range(1, amount + 1): dp[i] = float('inf') for coin in coins: if coin <= i: dp[i] = min(dp[i], dp[i - coin] + 1) return dp[amount] print(min_coins([1, 2, 5], 11))
Attempts:
2 left
💡 Hint
Check initialization of dp array and how min function is used.
✗ Incorrect
The code runs correctly and outputs 3, the minimum coins for 11.
🚀 Application
advancedMinimum coins for amount 15 with coins [1, 3, 7]
What is the minimum number of coins needed to make amount 15 using coins [1, 3, 7]?
DSA Python
def min_coins(coins, amount): dp = [float('inf')] * (amount + 1) dp[0] = 0 for i in range(1, amount + 1): for coin in coins: if coin <= i: dp[i] = min(dp[i], dp[i - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1 print(min_coins([1, 3, 7], 15))
Attempts:
2 left
💡 Hint
Try to combine 7, 3, and 1 coins to reach 15 with fewest coins.
✗ Incorrect
The minimum coins to make 15 are 7 + 7 + 1, which is 3 coins.
🧠 Conceptual
expertWhy does greedy fail for coin change minimum coins?
Why does a greedy approach (always picking the largest coin first) sometimes fail to find the minimum number of coins needed?
Attempts:
2 left
💡 Hint
Think about cases where picking the largest coin first leads to more coins overall.
✗ Incorrect
Greedy fails when picking the largest coin first leads to leftover amounts that require many small coins, while a combination of smaller coins could use fewer coins overall.
