Algorithms from zero
Greedy: multiple-choice review
Six questions that cut across the whole unit. Each one is a decision you make when you reach for greedy in anger — not a definition to recite, but the judgment call of whether a local choice is provably global, and what to do when it is not.
Confirm you can connect the greedy-choice property, the exchange argument, the sort-key decision for interval problems, and the failure signature of an unsafe greedy — the synthesis the four lessons built toward.
A teammate says greedy passed all 40 of their random tests, so it must be correct for this problem. Why is this reasoning unsound, and what would actually settle it?
You want the maximum number of non-overlapping meetings in one room. A colleague sorts by shortest duration (small meetings pack tightest). On [0,4], [3,5], [4,8] what does that produce, and why is the key wrong?
In the exchange argument for activity selection, you swap the optimum's first job X out and greedy's earliest-finishing job G in. Which single fact makes the swapped schedule still feasible?
For coin change with denominations {1, 3, 4} and amount 6, greedy returns 4+1+1 = 3 coins while DP returns 3+3 = 2. What does this mismatch tell you about the exchange argument, and what is the correct fallback?
Across the unit, what is the precise difference between greedy and dynamic programming on the same optimization problem?
In Gas Station, a colleague returns -1 the first time the running tank goes negative at some station i. Why is that wrong, and what is the correct logic?
The through-line of the unit is one discipline: a greedy algorithm always runs and always returns something, so the only question that matters is whether its local choice is provably global. The exchange argument (or stays-ahead) is how you prove safety; a missing swap — like coins 4 for 6 — is itself the proof of failure, and your fallback is DP. For interval problems the hard part is the sort key: earliest finish for maximization, start for merging, start-and-end sweep for rooms. And for scan greedies like Gas Station, never decide from a local dip — decide from the global invariant.