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 C# code that calculates the minimum number of coins needed to make amount 11 using coins {1, 2, 5}?
DSA C#
using System; class Program { static int MinCoins(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; for (int i = 1; i <= amount; i++) dp[i] = max; dp[0] = 0; for (int i = 1; i <= amount; i++) { foreach (int coin in coins) { if (coin <= i) { dp[i] = Math.Min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } static void Main() { int[] coins = {1, 2, 5}; int amount = 11; Console.WriteLine(MinCoins(coins, amount)); } }
Attempts:
2 left
💡 Hint
Think about the fewest coins to make 11 using 5, 2, and 1.
✗ Incorrect
The minimum coins to make 11 are 5 + 5 + 1 = 3 coins.
❓ Predict Output
intermediateMinimum coins for amount 7 with coins {2, 4}
What is the output of this C# code that finds the minimum coins needed to make amount 7 using coins {2, 4}?
DSA C#
using System; class Program { static int MinCoins(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; for (int i = 1; i <= amount; i++) dp[i] = max; dp[0] = 0; for (int i = 1; i <= amount; i++) { foreach (int coin in coins) { if (coin <= i) { dp[i] = Math.Min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } static void Main() { int[] coins = {2, 4}; int amount = 7; Console.WriteLine(MinCoins(coins, amount)); } }
Attempts:
2 left
💡 Hint
Check if 7 can be formed by sums of 2 and 4.
✗ Incorrect
7 cannot be formed by any combination of 2 and 4 coins, so output is -1.
🔧 Debug
advancedIdentify the error in coin change code
What error will this C# code produce when trying to find minimum coins for amount 5 with coins {1, 3, 4}?
DSA C#
using System; class Program { static int MinCoins(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount]; for (int i = 1; i <= amount; i++) dp[i] = max; dp[0] = 0; for (int i = 1; i <= amount; i++) { foreach (int coin in coins) { if (coin <= i) { dp[i] = Math.Min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } static void Main() { int[] coins = {1, 3, 4}; int amount = 5; Console.WriteLine(MinCoins(coins, amount)); } }
Attempts:
2 left
💡 Hint
Check array size and indexing in dp array.
✗ Incorrect
dp array size is amount (5), but code accesses dp[5], which is out of range (max index 4).
🧠 Conceptual
advancedWhy use dynamic programming for coin change?
Why is dynamic programming the best approach to solve the minimum coin change problem?
Attempts:
2 left
💡 Hint
Think about avoiding repeated work in overlapping subproblems.
✗ Incorrect
Dynamic programming stores results of smaller amounts to build up solutions efficiently.
🚀 Application
expertMinimum coins for amount 27 with coins {1, 5, 10, 25}
What is the minimum number of coins needed to make amount 27 using coins {1, 5, 10, 25}?
Attempts:
2 left
💡 Hint
Try to use the largest coins first and then smaller coins.
✗ Incorrect
The minimum is 3 coins: 25 + 1 + 1.
