Objective
Learn one argument that cracks a whole family of interview puzzles: count the outcomes the answer must distinguish, count what each test can reveal, and divide. It gives a lower bound instantly, often tells you what the first move must be, and sometimes shows a puzzle is impossible before you waste time on it.
The counting bound
A test with possible results, repeated times, produces at most different result sequences. If a strategy must tell apart possibilities, distinct possibilities need distinct result sequences, so
In entropy terms: each test reveals at most bits, and identifying one of equally likely possibilities needs bits. If the possibilities are not equally likely and you care about the expected number of tests, the bound becomes , and Huffman's algorithm (with -ary merges) gives the optimal adaptive strategy.
Three standard applications:
- Twenty questions. 20 yes/no questions distinguish at most objects. Conversely, finding a number in with yes/no questions needs of them; for that is 10.
- Higher, lower or correct. Guessing a number in where each guess returns "higher", "lower" or "correct" has three outcomes but "correct" ends the game, so the decision tree is a binary search tree: guesses cover at most numbers. For , 10 guesses suffice, since .
- Expected questions for a die. Identifying a fair die roll with yes/no questions needs at least questions on average; the best strategy for a single roll averages (Huffman lengths ).
A guessing variant appears in the practice problem on 100 flips. The key observation generalises: after yes/no questions your guess can depend only on the possible answer sequences, so you have at most distinct guesses, and among equally likely outcomes your chance of being right is at most .
Worked example
twelve coins, three weighings
Twelve coins look identical; one is counterfeit and is either heavier or lighter than the others. With a balance scale, find the counterfeit and say whether it is heavy or light in three weighings. Use counting to find the first weighing before solving.
Show solutionHide solution
Count. There are possibilities (which coin, heavy or light). A weighing has 3 results (left down, balance, right down), so three weighings give : not ruled out, but with only 3 to spare.
Constrain the first weighing. After it, each branch has two weighings left, which can distinguish at most possibilities. Weighing coins against :
- Balance leaves the other coins, each heavy or light: possibilities.
- Left down leaves "a left coin is heavy" or "a right coin is light": possibilities (the same for right down).
We need and , so and . The first weighing must be 4 against 4. Three against three leaves 12 possibilities on a balance, and five against five leaves 10 on a tilt, both too many.
Weighing 1: coins 1–4 against 5–8.
If it balances, the counterfeit is among 9–12 (8 possibilities) and coins 1–8 are genuine. Weigh 9, 10, 11 against 1, 2, 3 (genuine).
- Balance: coin 12 is fake; weigh it against coin 1 to see if it is heavy or light.
- Left down: one of 9–11 is heavy; weigh 9 against 10. The heavier is fake, or 11 if they balance.
- Right down: the same with "light".
If 1–4 go down (5–8 up), the 8 possibilities are "one of 1–4 heavy" or "one of 5–8 light", and coins 9–12 are genuine. Weigh 1, 2, 5 against 3, 6, 9.
- Left down: 1 heavy, 2 heavy or 6 light (3 possibilities). Weigh 1 against 2: the heavier is fake; if they balance, 6 is light.
- Right down: 3 heavy or 5 light. Weigh 3 against 9 (genuine): down means 3 heavy, balance means 5 light.
- Balance: 4 heavy, 7 light or 8 light. Weigh 7 against 8: the lighter is fake; if they balance, 4 is heavy.
If 5–8 go down, swap the roles of the two groups. Every branch splits its possibilities into groups of at most 3, then at most 1, as the counting demanded. (A brute-force check over all 24 cases confirms the strategy.)
Generalisation. With weighings and no extra coin known to be genuine, one can find the counterfeit and its direction among up to coins: 12 for , 39 for . Thirteen coins have possibilities, yet three weighings cannot do it, because the first weighing can never be balanced well enough; with one extra genuine coin the limit rises to . Counting gives necessary conditions, not always sufficient ones.
Worked example
poisoned bottles
One of 1,000 bottles is poisoned. A rat that drinks from it dies within a day, and a rat may drink from many bottles. (a) How many rats are needed to find the bottle in one day? (b) Now you have two days: rats drink at the start of day 1, and survivors may drink again at the start of day 2. How many rats are needed?
Show solutionHide solution
(a) Count. Each rat ends the day dead or alive: 1 bit. rats give outcome patterns, and we need , so ( is too few).
Construction. Number the bottles in binary (10 bits). Rat drinks from every bottle whose bit is 1. The set of dead rats spells out the poisoned bottle's number. Ten rats.
(b) Count again. Each rat now ends in one of three states: died on day 1, died on day 2, or survived. rats give patterns, and , so at least 7 rats are needed.
Construction. Write bottle numbers in base 3 with 7 digits. Rat drinks on day 1 from bottles whose digit is 1, and (if alive) on day 2 from bottles whose digit is 2. Death day 1, death day 2 or survival reads off digit as 1, 2 or 0.
The general lesson: more distinguishable results per test means fewer tests. Getting a third outcome out of each rat cuts the count from 10 to 7, because .
When counting is weak
The counting bound assumes each test can split the remaining possibilities evenly. Often it cannot, and the true answer is much larger.
Twenty-five horses (practice problem): five horses per race, finishing order only, find the fastest three in order. There are possible ordered top-threes ( bits), and a race reveals one of orders ( bits). Counting says at least races. A second, cheap argument does far better: any horse that never races could be the fastest, so all 25 must race, which needs at least 5 races. The true minimum is higher still and needs an adversary argument. Counting is a starting point, not the end.
How to use this in an interview
- State the number of possibilities and the number of results per test , and give as a lower bound immediately. Interviewers value hearing the bound before the construction.
- Use the bound to constrain the first move, as in the coin problem: each branch must leave at most possibilities.
- Build a strategy that splits possibilities as evenly as possible at every step.
- If you cannot reach the bound, look for why a test cannot split evenly (identical coins, "correct" ending the game, horses that have not raced) and look for a stronger argument.
Knowledge check
One of 1,000 bottles is poisoned. Rats can drink on day 1 and day 2, and a poisoned rat dies within the day it drinks, so each rat ends as 'died day 1', 'died day 2' or 'survived'. What is the minimum number of rats?
Knowledge check
With four weighings on a balance and no coin known to be genuine in advance, what is the largest number of coins among which you can always find a single counterfeit and say whether it is heavy or light?
Knowledge check
A fair coin is flipped 10 times. You may ask two yes/no questions of your choice (the second may depend on the first answer), then must guess the whole sequence. What is the highest achievable probability of a correct guess?
Knowledge check
You must find a number from 1 to 1,000. Each guess is answered 'higher', 'lower' or 'correct'. How many guesses do you need in the worst case with the best strategy?
Key takeaways
- Lower bound: tests with results each to distinguish possibilities; for expected tests, .
- Use the bound to force the first move: each branch must leave at most possibilities (4 against 4 for twelve coins).
- More outcomes per test means fewer tests: two days of rats turn 10 rats into 7.
- After yes/no questions you can make at most distinct guesses, which bounds any guess-the-outcome puzzle.
- Counting bounds are necessary, not sufficient: 13 coins in 3 weighings and 25 horses show that structure can force many more tests.
Practice problems
Finished reading?
Answer the 4 knowledge checks to complete this lesson (0 done).