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 code that uses a greedy approach to make change with coins [25, 10, 5, 1] for amount 30?
DSA Python
def greedy_coin_change(coins, amount): result = [] for coin in coins: while amount >= coin: amount -= coin result.append(coin) return result coins = [25, 10, 5, 1] change = greedy_coin_change(coins, 30) print(change)
Attempts:
2 left
💡 Hint
Think about how the greedy algorithm picks the largest coin first and subtracts it from the amount.
✗ 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.
🧠 Conceptual
intermediateWhy Greedy Fails for Certain Coin Sets
Why does the greedy coin change algorithm fail to find the minimum number of coins for the coin set [25, 10, 1] and amount 40?
Attempts:
2 left
💡 Hint
Consider how greedy picks coins and if that always leads to the fewest coins.
✗ Incorrect
Greedy picks 25 first, then 10, then 1 five times, totaling 7 coins, but optimal is 4 coins (10 + 10 + 10 + 10).
🔧 Debug
advancedIdentify the Error in Greedy Interval Scheduling
What error does the following code produce when trying to schedule maximum non-overlapping intervals using a greedy approach?
DSA Python
intervals = [(1,4), (3,5), (0,6), (5,7), (8,9), (5,9)] intervals.sort(key=lambda x: x[0]) result = [] end = 0 for interval in intervals: if interval[0] >= end: result.append(interval) end = interval[1] print(result)
Attempts:
2 left
💡 Hint
Think about which sorting key is needed for greedy interval scheduling.
✗ Incorrect
Greedy interval scheduling requires sorting intervals by their finish time, not start time. Sorting by start time leads to suboptimal results.
❓ Predict Output
advancedOutput of Fractional Knapsack Greedy Algorithm
What is the output of the following code that uses a greedy approach to solve the fractional knapsack problem?
DSA Python
def fractional_knapsack(values, weights, capacity): ratio = [(v/w, v, w) for v, w in zip(values, weights)] ratio.sort(key=lambda x: x[0], reverse=True) total_value = 0 for r, v, w in ratio: if capacity >= w: capacity -= w total_value += v else: total_value += r * capacity break return round(total_value, 2) values = [60, 100, 120] weights = [10, 20, 30] capacity = 50 print(fractional_knapsack(values, weights, capacity))
Attempts:
2 left
💡 Hint
Calculate value/weight ratio and pick items greedily until capacity is full.
✗ Incorrect
The ratios are 6.0, 5.0, and 4.0. Pick 10 weight of first item (60 value), then 20 weight of second (100 value), then 20 weight fraction of third (4.0 * 20 = 80). Total = 60 + 100 + 80 = 240.
🧠 Conceptual
expertWhen Does Greedy Algorithm Guarantee Optimal Solution?
Which condition below guarantees that a greedy algorithm will always produce an optimal solution?
Attempts:
2 left
💡 Hint
Think about the properties that allow greedy to work correctly.
✗ Incorrect
Greedy algorithms work optimally only if the problem has the greedy-choice property (local optimal choices lead to global optimum) and optimal substructure (optimal solution contains optimal subsolutions).
