Challenge - 5 Problems
Greedy Mastery
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 with coins of denominations 25, 10, 5, and 1 for the amount 30?
DSA Java
import java.util.*; public class GreedyCoinChange { public static void main(String[] args) { int[] coins = {25, 10, 5, 1}; int amount = 30; List<Integer> result = new ArrayList<>(); for (int coin : coins) { while (amount >= coin) { amount -= coin; result.add(coin); } } System.out.println(result); } }
Attempts:
2 left
💡 Hint
Think about how the greedy algorithm picks the largest coin first and subtracts it until it can't anymore.
✗ Incorrect
The greedy algorithm picks the largest coin less than or equal to the remaining amount. For 30, it picks 25 once, then 5 once, totaling 30. So the output list is [25, 5].
🧠 Conceptual
intermediateWhen Does Greedy Fail in Coin Change?
Given coin denominations of 1, 3, and 4, what is the minimum number of coins needed to make change for 6 using a greedy approach that always picks the largest coin possible first?
Attempts:
2 left
💡 Hint
Try to pick the largest coin first and see how many coins you end up with.
✗ Incorrect
Greedy picks 4 first, then 1, then 1, totaling 3 coins. But the optimal solution is 2 coins (3 + 3). So greedy fails here.
🔧 Debug
advancedIdentify the Bug in Greedy Interval Scheduling
Consider the following Java code that tries to select the maximum number of non-overlapping intervals using a greedy approach. What is the bug that causes it to fail for some inputs?
DSA Java
import java.util.*; class Interval { int start, end; Interval(int s, int e) { start = s; end = e; } } public class IntervalScheduling { public static void main(String[] args) { Interval[] intervals = { new Interval(1, 4), new Interval(3, 5), new Interval(0, 6), new Interval(5, 7), new Interval(8, 9), new Interval(5, 9) }; Arrays.sort(intervals, (a, b) -> a.start - b.start); List<Interval> selected = new ArrayList<>(); int lastEnd = -1; for (Interval interval : intervals) { if (interval.start >= lastEnd) { selected.add(interval); lastEnd = interval.end; } } System.out.println(selected.size()); } }
Attempts:
2 left
💡 Hint
Think about which sorting order guarantees the greedy choice is optimal.
✗ Incorrect
The greedy algorithm for interval scheduling must sort intervals by their end times to pick the earliest finishing intervals first. Sorting by start time can lead to suboptimal results.
🚀 Application
advancedGreedy Algorithm for Activity Selection Output
Given activities with start and end times: (1,2), (3,4), (0,6), (5,7), (8,9), (5,9), what is the output list of selected activities (by their start times) using the greedy algorithm that selects activities by earliest finish time?
DSA Java
import java.util.*; class Activity { int start, end; Activity(int s, int e) { start = s; end = e; } public String toString() { return "(" + start + "," + end + ")"; } } public class ActivitySelection { public static void main(String[] args) { Activity[] activities = { new Activity(1, 2), new Activity(3, 4), new Activity(0, 6), new Activity(5, 7), new Activity(8, 9), new Activity(5, 9) }; Arrays.sort(activities, (a, b) -> a.end - b.end); List<Activity> selected = new ArrayList<>(); int lastEnd = -1; for (Activity act : activities) { if (act.start >= lastEnd) { selected.add(act); lastEnd = act.end; } } System.out.println(selected); } }
Attempts:
2 left
💡 Hint
Select activities that finish earliest and do not overlap with previously selected ones.
✗ Incorrect
Sorting by end time and selecting compatible activities yields (1,2), (3,4), (5,7), (8,9).
🧠 Conceptual
expertWhy Greedy Algorithm Fails for 0/1 Knapsack Problem
Why does the greedy algorithm that picks items with the highest value-to-weight ratio first fail to always find the optimal solution for the 0/1 Knapsack problem?
Attempts:
2 left
💡 Hint
Think about the difference between fractional and 0/1 knapsack problems.
✗ Incorrect
Greedy works for fractional knapsack where items can be split, but fails for 0/1 knapsack because items are indivisible and picking the best ratio first may block better overall combinations.
