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 Calculation
What is the output of this Go code that calculates the minimum number of coins needed to make change for 11 using coins [1, 2, 5]?
DSA Go
package main import "fmt" func minCoins(coins []int, amount int) int { dp := make([]int, amount+1) for i := 1; i <= amount; i++ { dp[i] = amount + 1 for _, c := range coins { if c <= i && dp[i-c]+1 < dp[i] { dp[i] = dp[i-c] + 1 } } } if dp[amount] > amount { return -1 } return dp[amount] } func main() { coins := []int{1, 2, 5} amount := 11 fmt.Println(minCoins(coins, amount)) }
Attempts:
2 left
💡 Hint
Think about the fewest coins that add up to 11 using 1, 2, and 5.
✗ Incorrect
The minimum coins to make 11 are 5 + 5 + 1, which is 3 coins.
❓ Predict Output
intermediateMinimum Coins for Amount Not Possible
What is the output of this Go code when trying to make change for 3 using coins [2, 4]?
DSA Go
package main import "fmt" func minCoins(coins []int, amount int) int { dp := make([]int, amount+1) for i := 1; i <= amount; i++ { dp[i] = amount + 1 for _, c := range coins { if c <= i && dp[i-c]+1 < dp[i] { dp[i] = dp[i-c] + 1 } } } if dp[amount] > amount { return -1 } return dp[amount] } func main() { coins := []int{2, 4} amount := 3 fmt.Println(minCoins(coins, amount)) }
Attempts:
2 left
💡 Hint
Check if the amount can be formed by any combination of coins.
✗ Incorrect
3 cannot be formed by any combination of 2 and 4, so the function returns -1.
🔧 Debug
advancedIdentify the Bug in Coin Change Code
This Go code is supposed to find the minimum coins needed for amount 7 with coins [1, 3, 4]. What is the bug causing wrong output?
DSA Go
package main import "fmt" func minCoins(coins []int, amount int) int { dp := make([]int, amount+1) dp[0] = 0 for i := 1; i <= amount; i++ { dp[i] = amount for _, c := range coins { if c <= i && dp[i-c]+1 < dp[i] { dp[i] = dp[i-c] + 1 } } } if dp[amount] > amount { return -1 } return dp[amount] } func main() { coins := []int{1, 3, 4} amount := 7 fmt.Println(minCoins(coins, amount)) }
Attempts:
2 left
💡 Hint
Check the base case initialization for dp array.
✗ Incorrect
dp[0] must be 0 because zero coins are needed to make amount 0. Without this, dp[i-c] uses zero value 0 incorrectly.
🧠 Conceptual
advancedTime Complexity of Coin Change Minimum Coins
What is the time complexity of the dynamic programming solution for minimum coins problem with n coins and amount m?
Attempts:
2 left
💡 Hint
Consider the nested loops over amount and coins.
✗ Incorrect
The solution uses a loop over amount (m) and inside it loops over coins (n), so total O(n*m).
🚀 Application
expertMinimum Coins for Large Amount with Limited Coins
Given coins [1, 5, 10, 25] and amount 63, what is the minimum number of coins needed?
Attempts:
2 left
💡 Hint
Try to use the largest coins first to minimize count.
✗ Incorrect
63 = 25 + 25 + 10 + 1 + 1 + 1 = 6 coins total.
