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 Java code that finds the minimum number of coins needed to make amount 11 using coins {1, 2, 5}?
DSA Java
public class CoinChange { public static int minCoins(int[] coins, int amount) { int[] dp = new int[amount + 1]; for (int i = 1; i <= amount; i++) { dp[i] = amount + 1; for (int coin : coins) { if (coin <= i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } public static void main(String[] args) { int[] coins = {1, 2, 5}; int amount = 11; System.out.println(minCoins(coins, amount)); } }
Attempts:
2 left
💡 Hint
Think about the fewest coins to make 11 using 1, 2, and 5.
✗ Incorrect
The minimum coins to make 11 are 5 + 5 + 1 = 3 coins.
❓ Predict Output
intermediateOutput when no solution exists
What is the output of the code when coins are {2, 4} and amount is 7?
DSA Java
public class CoinChange { public static int minCoins(int[] coins, int amount) { int[] dp = new int[amount + 1]; for (int i = 1; i <= amount; i++) { dp[i] = amount + 1; for (int coin : coins) { if (coin <= i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } public static void main(String[] args) { int[] coins = {2, 4}; int amount = 7; System.out.println(minCoins(coins, amount)); } }
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 output is -1.
🔧 Debug
advancedIdentify the error in coin change code
What error does the following code produce when run?
DSA Java
public class CoinChange { public static int minCoins(int[] coins, int amount) { int[] dp = new int[amount]; dp[0] = 0; for (int i = 1; i <= amount; i++) { dp[i] = amount + 1; for (int coin : coins) { if (coin <= i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } public static void main(String[] args) { int[] coins = {1, 2, 5}; int amount = 11; System.out.println(minCoins(coins, amount)); } }
Attempts:
2 left
💡 Hint
Check the size of dp array and its indexing.
✗ Incorrect
dp array size is amount, but code accesses dp[amount], causing ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedWhy use dynamic programming for coin change?
Why is dynamic programming a good approach to solve the minimum coin change problem?
Attempts:
2 left
💡 Hint
Think about overlapping subproblems and optimal substructure.
✗ Incorrect
Dynamic programming stores results of smaller amounts to build up solution for larger amounts efficiently.
🚀 Application
expertMinimum coins for large amount with given coins
Given coins {1, 3, 4} and amount 6, what is the minimum number of coins needed?
Attempts:
2 left
💡 Hint
Try combinations like 3+3 or 4+1+1.
✗ Incorrect
Minimum coins for 6 are 3 + 3 = 2 coins.
