Challenge - 5 Problems
Greedy vs DP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Greedy Coin Change Algorithm
What is the output of the following Java code that uses a greedy approach to make change for 30 cents with coins 25, 10, 5, and 1?
DSA Java
int[] coins = {25, 10, 5, 1}; int amount = 30; int count = 0; for (int coin : coins) { count += amount / coin; amount %= coin; } System.out.println(count);
Attempts:
2 left
💡 Hint
Think about how many coins of each type the greedy method picks.
✗ Incorrect
The greedy method picks one 25-cent coin and one 5-cent coin, totaling 2 coins. But since 30 = 25 + 5, the count is 2. However, the code counts coins by dividing and summing: 30/25=1 coin, remainder 5; 5/10=0 coin; 5/5=1 coin; total 2 coins.
🧠 Conceptual
intermediateChoosing Between Greedy and Dynamic Programming
Which of the following statements best explains when to use a greedy algorithm instead of dynamic programming?
Attempts:
2 left
💡 Hint
Greedy algorithms work when making local optimal choices leads to a global optimum.
✗ Incorrect
Greedy algorithms work when the problem has optimal substructure and the greedy choice property, meaning local choices lead to a global optimum without needing to explore all subproblems.
🔧 Debug
advancedWhy Does This Greedy Algorithm Fail for Coin Change?
The following Java code tries to find the minimum number of coins for 6 cents using coins 1, 3, and 4. Why does it fail to find the minimum number of coins?
DSA Java
int[] coins = {4, 3, 1}; int amount = 6; int count = 0; for (int coin : coins) { count += amount / coin; amount %= coin; } System.out.println(count);
Attempts:
2 left
💡 Hint
Think about whether greedy always works for all coin sets.
✗ Incorrect
The greedy approach fails here because it picks one 4-cent coin and then two 1-cent coins (total 3 coins), but the optimal solution is two 3-cent coins (2 coins). Greedy does not explore all combinations.
❓ Predict Output
advancedDynamic Programming Output for Coin Change
What is the output of this Java code that uses dynamic programming to find the minimum number of coins for 6 cents with coins 1, 3, and 4?
DSA Java
int[] coins = {1, 3, 4}; int amount = 6; int[] dp = new int[amount + 1]; java.util.Arrays.fill(dp, amount + 1); dp[0] = 0; for (int i = 1; i <= amount; i++) { for (int coin : coins) { if (coin <= i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } } System.out.println(dp[amount]);
Attempts:
2 left
💡 Hint
Check how dp array builds up minimum coins for each amount.
✗ Incorrect
The dp array finds the minimum coins for 6 cents as 2 (two 3-cent coins). The code correctly computes dp[6] = 2.
🧠 Conceptual
expertIdentifying Optimal Substructure and Greedy Choice Property
Which problem below has both optimal substructure and the greedy choice property, making it suitable for a greedy algorithm?
Attempts:
2 left
💡 Hint
Greedy works well when local choices lead to global optimum without backtracking.
✗ Incorrect
Scheduling tasks to maximize non-overlapping intervals has optimal substructure and greedy choice property, so greedy algorithms work well. Other problems require dynamic programming or have no greedy property.
