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 C# code that uses a greedy approach to make change with coins of denominations 1, 5, and 10 for the amount 18?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int amount = 18; int[] coins = {10, 5, 1}; List<int> result = new List<int>(); foreach (int coin in coins) { while (amount >= coin) { amount -= coin; result.Add(coin); } } Console.WriteLine(string.Join(", ", result)); } }
Attempts:
2 left
💡 Hint
Think about how the greedy algorithm picks the largest coin first until it can't anymore.
✗ Incorrect
The greedy algorithm picks the largest coin (10) once, then the next largest (5) once, then uses 1 coins to complete the amount. So the output is 10, 5, 1, 1, 1.
🧠 Conceptual
intermediateWhy Greedy Fails for Certain Coin Systems
Why does the greedy algorithm fail to find the minimum number of coins for the amount 6 when the coin denominations are 1, 3, and 4?
Attempts:
2 left
💡 Hint
Consider how the greedy algorithm picks coins and compare with the optimal solution.
✗ Incorrect
The greedy algorithm picks the largest coin less than or equal to the remaining amount. For 6, it picks 4 first, then two 1s, totaling 3 coins. But the optimal solution is two 3s, totaling 2 coins.
🔧 Debug
advancedIdentify the Error in Greedy Interval Scheduling Code
What error does the following C# code produce when trying to select the maximum number of non-overlapping intervals using a greedy approach?
DSA C#
using System; using System.Collections.Generic; class Interval { public int start, end; public Interval(int s, int e) { start = s; end = e; } } class Program { static void Main() { List<Interval> intervals = new List<Interval> { new Interval(1, 3), new Interval(2, 5), new Interval(4, 7) }; intervals.Sort((a, b) => a.start - b.start); List<Interval> selected = new List<Interval>(); int lastEnd = 0; foreach (var interval in intervals) { if (interval.start >= lastEnd) { selected.Add(interval); lastEnd = interval.end; } } Console.WriteLine(selected.Count); } }
Attempts:
2 left
💡 Hint
Check how intervals are sorted and how selection is done.
✗ Incorrect
The code sorts intervals by start time and selects intervals greedily if they don't overlap. This approach works but is not optimal for interval scheduling. However, the code runs and outputs 2, which is the number of non-overlapping intervals selected.
❓ Predict Output
advancedOutput of Greedy Interval Scheduling with End-Time Sorting
What is the output of the following C# code that selects the maximum number of non-overlapping intervals by sorting intervals by their end time?
DSA C#
using System; using System.Collections.Generic; class Interval { public int start, end; public Interval(int s, int e) { start = s; end = e; } } class Program { static void Main() { List<Interval> intervals = new List<Interval> { new Interval(1, 3), new Interval(2, 5), new Interval(4, 7) }; intervals.Sort((a, b) => a.end - b.end); List<Interval> selected = new List<Interval>(); int lastEnd = 0; foreach (var interval in intervals) { if (interval.start >= lastEnd) { selected.Add(interval); lastEnd = interval.end; } } Console.WriteLine(selected.Count); } }
Attempts:
2 left
💡 Hint
Sorting by end time allows selecting more intervals.
✗ Incorrect
Sorting intervals by their end time and selecting intervals greedily ensures the maximum number of non-overlapping intervals are chosen. Here, it selects (1,3) and (4,7), outputting 2.
🧠 Conceptual
expertWhen Does Greedy Algorithm Guarantee Optimal Solution?
Which condition below guarantees that a greedy algorithm will always produce an optimal solution for a problem?
Attempts:
2 left
💡 Hint
Think about the two key properties that make greedy algorithms work.
✗ Incorrect
A greedy algorithm works optimally when the problem has the greedy-choice property (local optimal choices lead to global optimum) and optimal substructure (optimal solution contains optimal solutions to subproblems).
