Before you commit to a greedy solution in an interview, how do you convince yourself the greedy choice is safe?
answer
- Argue both for and against at once
- Assume an optimum without your choice
- Rewrite it to include your choice
- Which constraint makes the rewrite illegal?
- Smallest counterexamples are two or three items
basics
~10 sTwo moves: try to prove the greedy choice belongs to some optimal solution with an exchange argument, and simultaneously hunt for a small counterexample against exhaustive search. Passing the provided examples proves nothing.
solid answer
~50 sI attack it from both ends. Constructively, I attempt an exchange argument: assume an optimal solution that does not contain my greedy choice, and show I can rewrite it to include that choice without losing value. If that rewrite goes through, greedy is correct and I can say why. Destructively, I hunt for a counterexample on tiny inputs - two or three items, small numbers - checking greedy against brute force, because when greedy is wrong the smallest witness is usually minuscule and nobody puts it in the sample cases. I also read the statement for the word that carries the property: whether items split, whether weights are equal, whether input arrives sorted. If neither lands within a couple of minutes, I say so and formulate the exact method, because an unjustified greedy that merely passes the examples is the worst outcome.
go deeper
Learn the habit before the theory: whenever a greedy rule occurs to you, spend one minute trying to break it on two or three tiny items. Finding the break yourself is worth far more than being told about it.
Be able to run an exchange argument end to end and name the step that depends on a constraint. Say out loud which clause of the problem statement your justification rests on.
Show a decision procedure rather than a verdict: proof attempt and counterexample hunt in parallel, a stated time box, and a clear fallback to an exact formulation when neither lands. On the job, mention differential testing of greedy against brute force on small random inputs.
Own the risk framing. An unjustified greedy in production is a silent wrongness that passes review and monitoring alike, so make the justification an artefact - a proof note or a property test - rather than a claim someone made once in a design discussion.
## The failure this question exists to prevent The most common way a candidate loses a paradigm question is not writing a wrong loop. It is writing a *plausible* greedy, watching it pass the two examples printed in the statement, and declaring it correct. Greedy heuristics agree with the optimum on most random inputs; that is exactly why they are tempting and exactly why sample cases do not test them. A discipline for deciding is therefore worth more than any single remembered verdict. ## Move one: the exchange argument The greedy-choice property says that the first move greedy makes is contained in *some* optimal solution. The standard way to establish it is an exchange argument, and it has a fixed shape: 1. Let `OPT` be any optimal solution that does *not* contain the greedy choice `g`. 2. Show a transformation of `OPT` that inserts `g` and removes something else. 3. Show the transformed solution is still legal (respects the constraints). 4. Show its objective value is no worse. If all four steps hold, then an optimal solution containing `g` exists, greedy's first move is safe, and the same reasoning applies recursively to what remains - that recursion is the optimal-substructure half of the story. Step 3 is where these proofs actually die, and it is where you should look first. In the divisible-cargo problem, swapping one unit of weight for another keeps the load legal because the weight is unchanged; make the items indivisible and the swap must move a whole item, the weight changes, and the argument collapses. The proof does not fail vaguely - it fails at an identifiable line, and being able to point at that line is the senior version of this answer. ## Move two: hunt the counterexample Run the two moves in parallel, because the counterexample search often finishes first. Two properties make it cheap: - **Counterexamples are tiny.** Greedy's failures show up at two or three items, or at small amounts. The change-making failure at denominations 1, 3, 4 needs an amount of 6. The whole-crate failure needs two crates. - **Brute force is available at that size.** Enumerate every subset or every decomposition for inputs that small and compare against the greedy answer. In an interview, do this by hand with deliberately adversarial numbers: make one option locally attractive and globally blocking - the item that is densest but strands capacity, the coin that is largest but wrecks the remainder. If you can build such a case in under a minute, greedy is dead and you have saved yourself twenty minutes. On the job the same idea becomes a randomised differential test: generate small random inputs, compare greedy against the exact method, and keep the first disagreement as a permanent regression case. ## Move three: read the statement for the load-bearing word Paradigm choice usually hangs on one clause, and interviewers flip it on purpose to see whether you re-derive or recite. Words that carry the property: - **"May be split" / "whole units only."** Divisibility makes the unit-for-unit exchange legal, which is the difference between a provable greedy and a hard optimisation. - **"All the same size / weight / duration."** Uniformity often collapses a hard problem into a counting one where greedy is trivially right. - **"Already sorted" or "arrives in order."** Ordering can supply the very structure the exchange argument needs. - **"Choose at most one from each group" / "unlimited copies."** Reuse rules change which exchanges are available. When the interviewer changes one of these mid-problem, the correct reaction is not to patch your greedy. It is to say which step of your proof just stopped holding, and therefore which paradigm now applies. That single sentence is often the whole point of the follow-up. ## What a good answer sounds like "My greedy choice is the densest item. To justify it I would take any optimal load without that item and swap weight into it - that keeps the load legal only because items can be split, so the argument depends on divisibility. If items were whole, I would expect a two-item counterexample, and I would look for one where the densest item strands capacity. Failing to find either a proof or a counterexample quickly, I would formulate the exact method rather than ship an unjustified greedy." That answer demonstrates the thing the question is testing: paradigm choice as a decision procedure with evidence, not as a guess with confidence attached. ## Two claims to avoid First, greedy and an exact method are not interchangeable implementations of the same idea, with greedy merely faster - when the greedy-choice property fails, greedy is simply wrong, at any speed. Second, an exchange argument that works for one problem does not transfer by resemblance; near-identical statements differing in one clause land on opposite sides of the line, which is precisely why the check has to be re-run every time.
- The interviewer changes one word: the crates may now be split open and repacked. What changes?The exchange argument becomes legal, so the paradigm flips. With splitting, I can move a unit of weight between loads without changing the total weight, which proves the value-density greedy optimal and reduces the problem to a sort. I would say that explicitly - not that greedy now 'works', but that the specific proof step that was blocked is now available.
- What exactly does an exchange argument prove - that every optimal solution contains the greedy choice?No, only that *some* optimal solution does. That is all greedy needs: it never has to reproduce a particular optimum, just to stay on a path to one. Claiming every optimum contains the greedy choice is a stronger statement that is usually false, since ties and symmetric alternatives are common. The distinction matters when you are asked to justify the recursion.
- Your greedy passes every provided example. What have you actually learned?Essentially nothing about optimality. Greedy heuristics agree with the optimum on most inputs, and the sample cases are chosen to illustrate the problem rather than to break candidate solutions. Evidence that would count is a proof of the greedy-choice property, or an exhaustive comparison against brute force over all small inputs. Anything else is a coincidence you are choosing to trust.
saying these in an interview costs you the question
- Treats passing the sample cases as proof of correctness
- Calls greedy a faster version of the exact method
- Never attempts a counterexample on tiny inputs
- Claims every optimal solution must contain the greedy choice
- Transfers a proof from a similar-looking problem without rechecking
- Patches the greedy rule when a constraint is flipped