Quant Memo
Core

Mastermind: Deduction From Feedback

In the code-breaking game Mastermind, each guess returns only a count of exact and partial matches — and the discipline of using that sparse feedback to eliminate whole families of remaining codes, rather than guessing randomly, is the puzzle's real lesson.

Prerequisites: How to Attack a Brainteaser

One player picks a secret sequence of 4 pegs, each one of 6 possible colours (colours may repeat). The other player guesses a sequence and, after each guess, is told only two numbers: how many pegs are the right colour in the right position (black pegs) and how many are the right colour but the wrong position (white pegs) — never which specific pegs. The guesser must identify the secret in as few guesses as possible using only this coarse feedback. With 64=12966^4 = 1296 possible codes, how do you make real progress from feedback that never tells you exactly what's wrong?

The core idea: feedback prunes the candidate set, it doesn't reveal the answer directly

Every guess-and-feedback pair is a filter: "every code that would not have produced this exact (black, white) score against my guess is eliminated." Since you know the scoring rule exactly, you can — in principle — check any candidate code against the feedback received so far and discard it if it's inconsistent. The skill isn't decoding the feedback directly into pegs; it's choosing guesses that split the remaining candidate set as evenly and informatively as possible, so a small number of guesses collapses 1296 possibilities down to one.

Worked example: narrowing candidates after two guesses

Suppose the secret is unknown to you, and colours are numbered 1–6.

Guess 1: 1 1 2 2. Feedback: 1 black, 1 white. This rules out an enormous number of codes at once — for instance, any code with zero 1s and zero 2s anywhere is immediately eliminated (it would score 0 black, 0 white), and so is any code with exactly one 1 in position 1 and exactly one 2 in position 3 or 4 with no other 1s or 2s (that would score higher). Rather than hand-checking all 1296, treat this as a computational filter: only candidates whose score-against-"1122" equals exactly (1,1) survive.

Guess 2: 3 3 4 4. Feedback: 0 black, 2 white. Combined with guess 1's result, this tells you the secret contains no 3s or 4s in the right positions relative to this guess, but does contain some 3s and/or 4s somewhere, totaling 2 matched-by-colour-only. Cross this filter against the survivors of guess 1: keep only codes consistent with both scores simultaneously.

After just these two guesses, the surviving candidate set — codes consistent with (1 black, 1 white) against 1122 and (0 black, 2 white) against 3344 — typically shrinks from 1296 to a small double-digit number, because each guess is a joint constraint across all 4 positions and both count types at once, not a single yes/no bit. Continuing to guess codes that are maximally informative against the shrinking survivor set (a strategy famously formalized by Knuth as always minimizing the worst-case remaining candidate count) solves any secret within 5 guesses.

start 1296 after guess 1 ~90 after guess 2 ~15 ... each guess is a joint filter across all four positions and both match counts at once
Each (black, white) feedback pair eliminates every candidate code inconsistent with it; a well-chosen guess shrinks the surviving set sharply even though the feedback itself is just two small numbers.

Mastermind feedback is coarse (two counts, not positions), but each guess is a joint constraint over the entire code, so a well-chosen guess can eliminate the large majority of remaining candidates at once. The winning approach tracks the full surviving candidate set and picks each next guess to shrink it as much as possible in the worst case.

What this means in practice

The discipline of "maintain the full set of hypotheses still consistent with all evidence so far, and choose the next test to maximize information regardless of outcome" is the same logic behind a good sequential hypothesis test or an adaptive audit: you don't chase the most recent data point in isolation, you check it against everything you've already ruled in or out. Interviewers use Mastermind-style questions to see whether a candidate defaults to random guessing or to deliberately information-maximizing choices once feedback starts arriving.

A common mistake is treating each guess's feedback in isolation, updating a mental guess about individual peg colours directly from the latest score. The score is a property of the whole guess against the whole secret, not of individual pegs, so it must be checked against the entire remaining candidate set jointly, not decoded position by position.

Related concepts

Practice in interviews

Further reading

  • Knuth, The Computer as Master Mind (1976)
ShareTwitterLinkedIn