Skip to content
LeetQuant
E03 Information Theory

E03 · Information Theory · Lesson 4 of 4

Information-Theoretic Puzzles

Counting outcomes to get lower bounds for weighing, guessing and testing puzzles: the 12-coin problem, twenty questions, binary search with three answers, poisoned bottles, and when counting bounds are far from tight.

17 min

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 rr possible results, repeated kk times, produces at most rkr^k different result sequences. If a strategy must tell apart NN possibilities, distinct possibilities need distinct result sequences, so

rk≥N⟹k≥⌈log⁡rN⌉.r^k \ge N \quad\Longrightarrow\quad k \ge \lceil \log_r N \rceil .

In entropy terms: each test reveals at most log⁡2r\log_2 r bits, and identifying one of NN equally likely possibilities needs log⁡2N\log_2 N bits. If the possibilities are not equally likely and you care about the expected number of tests, the bound becomes E[k]≥H(X)/log⁡2rE[k] \ge H(X)/\log_2 r, and Huffman's algorithm (with rr-ary merges) gives the optimal adaptive strategy.

Three standard applications:

  • Twenty questions. 20 yes/no questions distinguish at most 220=1,048,5762^{20} = 1{,}048{,}576 objects. Conversely, finding a number in 1..N1..N with yes/no questions needs ⌈log⁡2N⌉\lceil\log_2 N\rceil of them; for N=1000N = 1000 that is 10.
  • Higher, lower or correct. Guessing a number in 1..N1..N 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: kk guesses cover at most 2k−12^k - 1 numbers. For N=1000N = 1000, 10 guesses suffice, since 210−1=10232^{10} - 1 = 1023.
  • Expected questions for a die. Identifying a fair die roll with yes/no questions needs at least log⁡26≈2.585\log_2 6 \approx 2.585 questions on average; the best strategy for a single roll averages 8/3≈2.6678/3 \approx 2.667 (Huffman lengths 2,2,3,3,3,32, 2, 3, 3, 3, 3).

A guessing variant appears in the practice problem on 100 flips. The key observation generalises: after kk yes/no questions your guess can depend only on the 2k2^k possible answer sequences, so you have at most 2k2^k distinct guesses, and among MM equally likely outcomes your chance of being right is at most 2k/M2^k / M.

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 solution

Count. There are 12×2=2412 \times 2 = 24 possibilities (which coin, heavy or light). A weighing has 3 results (left down, balance, right down), so three weighings give 33=27≥243^3 = 27 \ge 24: 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 32=93^2 = 9 possibilities. Weighing kk coins against kk:

  • Balance leaves the other 12−2k12 - 2k coins, each heavy or light: 2(12−2k)2(12 - 2k) possibilities.
  • Left down leaves "a left coin is heavy" or "a right coin is light": 2k2k possibilities (the same for right down).

We need 2(12−2k)≤92(12 - 2k) \le 9 and 2k≤92k \le 9, so k≥4k \ge 4 and k≤4k \le 4. 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 nn weighings and no extra coin known to be genuine, one can find the counterfeit and its direction among up to (3n−3)/2(3^n - 3)/2 coins: 12 for n=3n = 3, 39 for n=4n = 4. Thirteen coins have 26≤2726 \le 27 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 (3n−1)/2=13(3^n - 1)/2 = 13. 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 solution

(a) Count. Each rat ends the day dead or alive: 1 bit. kk rats give 2k2^k outcome patterns, and we need 2k≥10002^k \ge 1000, so k≥10k \ge 10 (29=5122^9 = 512 is too few).

Construction. Number the bottles 0..9990..999 in binary (10 bits). Rat jj drinks from every bottle whose bit jj 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. kk rats give 3k3^k patterns, and 36=729<1000≤37=21873^6 = 729 < 1000 \le 3^7 = 2187, so at least 7 rats are needed.

Construction. Write bottle numbers in base 3 with 7 digits. Rat jj drinks on day 1 from bottles whose digit jj is 1, and (if alive) on day 2 from bottles whose digit jj is 2. Death day 1, death day 2 or survival reads off digit jj 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 log⁡31000≈6.29\log_3 1000 \approx 6.29.

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 25×24×23=13,80025 \times 24 \times 23 = 13{,}800 possible ordered top-threes (≈13.75\approx 13.75 bits), and a race reveals one of 5!=1205! = 120 orders (≈6.91\approx 6.91 bits). Counting says at least ⌈13.75/6.91⌉=2\lceil 13.75 / 6.91 \rceil = 2 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

  1. State the number of possibilities NN and the number of results per test rr, and give ⌈log⁡rN⌉\lceil \log_r N \rceil as a lower bound immediately. Interviewers value hearing the bound before the construction.
  2. Use the bound to constrain the first move, as in the coin problem: each branch must leave at most rk−1r^{k-1} possibilities.
  3. Build a strategy that splits possibilities as evenly as possible at every step.
  4. 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: k≥⌈log⁡rN⌉k \ge \lceil \log_r N \rceil tests with rr results each to distinguish NN possibilities; for expected tests, E[k]≥H/log⁡2rE[k] \ge H/\log_2 r.
  • Use the bound to force the first move: each branch must leave at most rk−1r^{k-1} possibilities (4 against 4 for twelve coins).
  • More outcomes per test means fewer tests: two days of rats turn 10 rats into 7.
  • After kk yes/no questions you can make at most 2k2^k 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).