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 Go code that uses a greedy approach to make change for 30 cents with coin denominations 25, 10, and 1?
DSA Go
package main import "fmt" func main() { coins := []int{25, 10, 1} amount := 30 result := []int{} for _, coin := range coins { for amount >= coin { amount -= coin result = append(result, coin) } } fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about how the greedy algorithm picks the largest coin first and then moves to smaller coins.
✗ Incorrect
The greedy algorithm picks the largest coin less than or equal to the remaining amount. For 30 cents, it picks 25 once, then uses five 1-cent coins to make the remaining 5 cents.
🧠 Conceptual
intermediateWhen Does Greedy Fail in Interval Scheduling?
In interval scheduling, which greedy strategy can fail to find the maximum number of non-overlapping intervals?
Attempts:
2 left
💡 Hint
Consider which strategy might select intervals that block many others.
✗ Incorrect
Picking the shortest interval first can block many other intervals, leading to fewer total intervals selected. The correct greedy strategy is to pick intervals by earliest finish time.
🔧 Debug
advancedIdentify the Error in Greedy Activity Selector Code
What error does the following Go code produce when trying to select maximum activities using a greedy approach based on earliest finish time?
DSA Go
package main import "fmt" func main() { start := []int{1, 3, 0, 5, 8, 5} finish := []int{2, 4, 6, 7, 9, 9} n := len(start) selected := []int{0} last := 0 for i := 1; i < n; i++ { if start[i] >= finish[last] { selected = append(selected, i) last = i } } fmt.Println(selected) }
Attempts:
2 left
💡 Hint
Check if the intervals are sorted by finish time before selection.
✗ Incorrect
The code assumes intervals are sorted by finish time, but they are not. However, the given input is already sorted by finish time, so the code runs correctly and outputs [0 1 3 4].
🚀 Application
advancedGreedy Algorithm Failure in Fractional Knapsack Variant
Given items with weights and values, which greedy approach fails to find the optimal solution in the 0/1 knapsack problem?
Attempts:
2 left
💡 Hint
Greedy by value-to-weight ratio works for fractional knapsack but not always for 0/1 knapsack.
✗ Incorrect
The greedy approach picking items by highest value-to-weight ratio works for fractional knapsack but can fail for 0/1 knapsack because it cannot take fractions and may miss better combinations.
🧠 Conceptual
expertWhy Greedy Algorithms Guarantee Optimality in Matroid Problems
Which property of matroids ensures that a greedy algorithm always finds the optimal solution?
Attempts:
2 left
💡 Hint
Matroids have special properties that allow greedy choices to be extended to optimal solutions.
✗ Incorrect
Matroids have the hereditary property (subsets of independent sets are independent) and the exchange property (elements can be exchanged to maintain independence), which guarantee greedy algorithms find optimal solutions.
