skip to content

A teammate balances two shards by sorting record sizes descending and alternating — how do you disprove it?

level: seniorimportance: should knowfreq 38%

answer

  1. a proof needs every input; you need one
  2. start with the smallest instance that can differ
  3. three records, already sorted descending
  4. what if the two smaller ones together stay light?
  5. green tests sample, they do not quantify

basics

~20 s

Produce one concrete counterexample. Sizes 3, 2, 2 alternate into shards of 5 and 2, while 3 against 2 plus 2 gives a larger shard of only 4. A single instance settles it; a passing test suite never could.

solid answer

~50 s

Disproof is cheap and constructive: search the smallest instances first. With sizes `3, 2, 2` the rule assigns the first and third records together, giving loads 5 and 2, while the obvious split gives 3 and 4 — a larger shard of 4 instead of 5. That is the whole review comment. The systematic method behind it: try `n` of 3 or 4, then push structural extremes (all sizes equal, one record dominating the rest, totals that split exactly evenly), and if hand search fails, brute-force every partition for small `n` and diff it against the rule over random instances. Then be honest about what a *failed* search means: no counterexample found is not a proof. Either the author supplies an exchange or stays-ahead argument, or the rule ships as a documented approximation with a stated bound and monitored skew.

code

pseudocode · 10 lines
pseudocode
// goal: minimise the larger shard's total load
sort sizes descending
loadA = 0
loadB = 0
for i in 0..n-1:
    if i mod 2 == 0:
        loadA = loadA + sizes[i]
    else:
        loadB = loadB + sizes[i]
return max(loadA, loadB)

go deeper

for a junior

Remember that one concrete failing input settles the question, and that passing tests never do. Practise building three-element instances by hand before reaching for a script.

for a middle

Explain the search order — smallest instances, then structural extremes, then exhaustive comparison against a brute-force answer — and why ties and dominating elements are the productive corners.

for a senior

Own the review outcome, not just the counterexample: say which of proof, disproof or documented approximation you expect back, and recognise when the missing proof reflects a genuine hardness obstruction rather than a fixable rule.

for a principal

Set the team's standard for unproven heuristics: what guarantee must be written down, which skew metric is monitored in production, and how much engineering time a correctness argument is worth against the cost of an imbalanced shard.

## The claim under review A teammate proposes splitting sized records into two shards by sorting sizes descending and dealing them out alternately, aiming to minimise the larger shard's total load. They report that it passes their tests. Your job as reviewer is to decide whether the rule is *correct*, and "the tests are green" does not answer that question — a test suite samples inputs, while a correctness claim quantifies over all of them. ## Finding the counterexample Start with the smallest instance that can possibly differ. With one or two records every rule agrees. With three, the alternating rule pairs the first and third records against the second alone, and that pairing is exactly where it fails: it can be beaten whenever the two smaller records together are lighter than one big one plus a small one. Sizes `3, 2, 2`, already sorted descending. Alternating gives shard A = `3 + 2 = 5` and shard B = `2`, so the larger load is 5. But splitting `3` against `2 + 2` gives loads 3 and 4, a larger load of 4. Since a valid split with larger-load 4 exists, the rule is not optimal. One instance, three numbers, done — and small enough to paste into a review comment and to keep as a regression test. ## The search method, generalised Counterexample hunting is a craft with a repeatable order of attack: 1. **Smallest first.** Most greedy heuristics break at `n` of 3 or 4. Minimal counterexamples are easier to explain, easier to turn into a test, and usually expose the structural flaw rather than an accident. 2. **Structural extremes.** All values equal; one value dominating the sum of the rest; values that admit an exact even split; values that force ties in the sort. Ties and exact splits are where tie-breaking rules quietly decide the answer. 3. **Differential testing.** When hand search stalls, enumerate every possible assignment for small `n` (exhaustive for `n` up to roughly a dozen) and compare against the heuristic on random and adversarial inputs. This is the mechanical version of the same search, and it finds what intuition misses. 4. **Read the failed proof.** If you tried an exchange argument and the swap could not be made at some position, that position tells you what the counterexample must look like. Proof attempts and counterexample searches are the same activity approached from two directions. ## What a *failed* search proves Nothing. This is the point most reviews get wrong in the other direction: after a million random instances with no violation, the rule is still unproven. Counterexamples cluster in structured corners — exact ties, dominating elements, exact-split totals — that uniform random sampling rarely produces. The absence of a found counterexample is weak evidence, not a theorem. So the review has exactly three acceptable outcomes: a counterexample (the rule is wrong), a proof (an exchange or stays-ahead argument, written down), or an explicit downgrade — the rule is accepted as a heuristic with a stated guarantee and a monitored metric. ## The deeper reason no patch will fix it Before proposing a better tie-break, ask whether an exact greedy can exist at all. Deciding whether a set of sizes can be split into two equally-loaded halves is a classic NP-hard problem, and minimising the larger load is the optimisation version of it. So no efficient rule — greedy or otherwise — is exactly optimal on all inputs unless a famous open question resolves surprisingly. That reframes the review completely: the goal is not a correct greedy, it is a greedy with a known bound. The standard repair is to assign each record, largest first, to whichever shard is currently lighter. On the `3, 2, 2` instance it produces the optimal 3-versus-4 split, and in general this descending-order least-loaded rule is a well-studied approximation with a proven constant-factor guarantee — never worse than about a third above optimal for any number of shards, and tighter for two. It is still not exact, and saying so is the honest review comment. ## Writing the review A good comment does three things in three lines: gives the counterexample with concrete numbers, names what is actually being claimed (exact optimality versus an approximation), and states which of the three outcomes you expect back. Avoid "this feels wrong" — a counterexample ends the discussion, an intuition starts a longer one.

  • Your random search finds no counterexample after a million instances. Is the rule correct?
    No. Absence of a counterexample in a sampled space is weak evidence, not a theorem. Counterexamples cluster in structured corners — exact ties, a dominating element, totals that split evenly — that uniform random generation rarely lands on. Ask for an exchange or stays-ahead argument, or accept the rule explicitly as a heuristic with a stated bound.
  • How large should the instances in your counterexample search be?
    Start at three or four elements and grow only if everything small is fine. Minimal counterexamples are easier to explain in review, cheap to keep as regression tests, and they usually expose the structural flaw rather than an incidental one. Large random instances tend to hide the reason a rule fails.
  • The counterexample exists — do you patch the greedy or change the approach?
    First ask whether an exact rule can exist. Minimising the larger shard's load is NP-hard, so no efficient rule is exactly optimal and patching the tie-break just moves the failing instance. The honest outcome is a greedy accepted as an approximation with a stated guarantee and a monitored skew metric, not a fourth attempt at the rule.
  • How is searching for a counterexample related to attempting the proof?
    They are the same activity from opposite ends. When an exchange argument stalls because a swap breaks feasibility or loses value at some position, that position describes the shape of the counterexample. Alternating between the two is the fastest way to resolve a greedy you are unsure about.

saying these in an interview costs you the question

  • Treats a green test suite as evidence of optimality
  • Argues the rule is wrong without producing an instance
  • Searches only large random inputs, never tiny ones
  • Concludes correctness because no counterexample was found
  • Patches the tie-break without asking if an exact rule can exist
  • Confuses an approximation guarantee with exact optimality

context