The objective
"We take turns removing coins; who wins?" is a favourite interview question because it tests whether you can reason backwards rather than simulate forwards. The objective is to classify any position of a simple game as a win or a loss for the player to move, find the winning move, and recognise the two classic proof tricks, strategy stealing and pairing.
P-positions and N-positions
Consider games with two players alternating, perfect information, no chance, finitely many positions, and the normal play rule: the player who cannot move loses (equivalently, whoever makes the last move wins). Every position is one of two kinds:
- P-position: the Previous player (who just moved) wins with best play. The player to move loses.
- N-position: the Next player (to move) wins.
They are defined by backward induction:
- A terminal position (no moves) is P.
- A position is N if some move leads to a P-position.
- A position is P if every move leads to an N-position.
To win, always move to a P-position. Your opponent then faces only moves to N-positions, and you repeat. The method in an interview: compute P/N for small positions, spot the pattern, then prove it by checking rules 2 and 3.
Take-away games
A pile of counters; a move removes of them; the player who takes the last counter wins. The P-positions are exactly the multiples of :
- From a multiple of , any removal of to leaves a non-multiple (rule 3).
- From a non-multiple, remove to reach a multiple (rule 2).
The winning strategy is a pairing in time: whenever the opponent takes , you take , so each round removes exactly .
With other move sets the pattern is less obvious but still periodic. If you may remove 1, 3 or 4, the table gives P-positions : is P exactly when . Build the table, do not guess.
Worked example
21 counters, take one to three
There are 21 counters. Players alternately remove 1, 2 or 3; whoever takes the last counter wins. Do you want to go first, and how do you play? What changes if you may take 1, 3 or 4 instead?
Show solutionHide solution
Small cases. With 0 counters the player to move has lost (P). With 1, 2 or 3 you take everything (N). With 4, every move leaves 1, 2 or 3, all N, so 4 is P. Then 5, 6, 7 are N (move to 4) and 8 is P. The pattern is multiples of 4.
Answer. is not a multiple of 4, so go first and take 1, leaving 20. After every opponent move of , take ; the pile goes at the end of each of your turns, and you take the last counter.
Moves 1, 3, 4. The table of P-positions is , i.e. . Check the start: is P; is N (take 1); is P (only move is to 1, which is N); and are N (take 1 or 4 to reach 2 or 0); is N (take 3, reach 2); is N (take 4, reach 2); is P (moves reach 6, 4, 3, all N). Since , the first player now loses with best play, so you should let the opponent start.
Nim and the nim-sum
In Nim there are several piles; a move removes any positive number of counters from one pile; whoever takes the last counter wins. Write the pile sizes in binary and XOR them (add each bit column mod 2, no carries). The result is the nim-sum .
Bouton's theorem. A Nim position is P if and only if its nim-sum is 0.
Check the two rules.
- From , every move gives . A move changes exactly one pile from to . The new nim-sum is .
- From , some move gives . Let be the highest set bit of . Some pile has bit set (otherwise that column would XOR to 0). Then , because bit switches off and only lower bits change. Reduce pile to ; the new nim-sum is .
The second bullet is also the algorithm for the winning move: compute , find a pile with 's top bit set, and reduce it to .
Why Nim matters beyond Nim. By the Sprague–Grundy theorem, every impartial normal-play game position has a Grundy value (the smallest non-negative integer not among the values of the positions it can move to), and a sum of independent games is a P-position exactly when the XOR of the Grundy values is 0. A take-away pile with moves has Grundy value , so several such piles played together are solved by XOR-ing those residues.
Misère Nim (last mover loses) is played identically until your move would leave only piles of size 1; then leave an odd number of them.
Worked example
Nim with piles 3, 4, 5
Nim with piles of 3, 4 and 5. Should you move first, and if so, what is the winning move?
Show solutionHide solution
In binary: , , . Column by column: , then . The nim-sum is , so the first player wins.
The top bit of is the 2s bit. Only the pile of 3 has it set. Reduce that pile to , i.e. take 2 from the pile of 3, leaving : .
The other piles fail the test: and , which would mean adding counters. A brute-force search over all moves confirms that taking 2 from the 3-pile is the only winning move. From the opponent must break the zero sum and you restore it each turn.
Strategy stealing
Sometimes you can prove the first player wins without finding a strategy. The template: suppose the second player has a winning strategy; show the first player could use it, a contradiction. It needs two ingredients: the game cannot end in a draw, and an extra move never hurts the player who makes it (or the first player can make a move that the second player's strategy can absorb).
Chomp. A rectangular chocolate bar of squares, not , has a poisoned square in the top-left corner. A move picks a square and eats it together with every square below and to the right of it. Whoever eats the poisoned square loses. Claim: the first player wins.
Suppose not, so the second player wins. Let the first player eat only the bottom-right square. The second player has a winning reply, eating some square and leaving position . But any bite containing also removes the bottom-right square, so is reachable in one move from the full bar. The first player could have moved to directly and then copied the second player's winning strategy. Contradiction. The proof gives no hint of the winning move, which for general rectangles is still unknown.
Hex. Hex cannot end in a draw (a full board always has a winning chain for one player), and an extra stone of your colour can never hurt you. If the second player had a winning strategy, the first player could place a stone anywhere, then pretend to be the second player and follow that strategy; when the strategy demands the cell holding the extra stone, play anywhere else. So the first player wins on every board. The same argument shows the first player can never lose at tic-tac-toe (draws are allowed there, so you get only "does not lose").
Pairing and symmetry strategies
A pairing strategy answers each opponent move with a matched move that preserves a symmetry, so whenever the opponent can move, so can you.
Coins on a round table. Players alternately place identical coins flat on a round table, without overlapping; the first player unable to place a coin loses. The first player places a coin exactly at the centre, then answers every opponent coin with one at the point reflected through the centre. The reflected spot is always free: the position is symmetric after each of the first player's moves, and the opponent's new coin cannot overlap its own reflection because a coin overlapping its reflection would have to cover the centre, which is taken. So the first player always has a move and wins. The centre is needed because it is the one spot that is its own reflection.
Coins in a row. An even number of coins lie in a row; players alternately take a coin from either end, and the larger total wins. Number the positions . The first player can take all the odd-numbered coins or all the even-numbered ones: taking an end of one parity always leaves both ends of the other parity for the opponent. So the first player can guarantee at least half the total and never loses. This is a guarantee, not the optimum: with coins the parity strategy gives , while optimal play (computed by dynamic programming) gets 15 by taking the 10, then the 5 after the opponent takes the 7.
Knowledge check
A pile has 30 counters. Players alternately remove 1 to 4 counters, and whoever takes the last counter wins. What should the first player do?
Knowledge check
Nim with piles 2, 5 and 6. What is the winning move?
Knowledge check
Nim with piles 1, 2 and 3. Who wins with best play?
Knowledge check
What does the strategy-stealing argument for Chomp establish?
Key takeaways
- Label positions P (player to move loses) and N (player to move wins) by backward induction: N if some move reaches P, P if every move reaches N.
- Take-away with moves : P-positions are multiples of ; for other move sets, tabulate and find the period.
- Nim: P exactly when the XOR of the piles is 0; to win, reduce a pile containing the nim-sum's top bit to .
- Strategy stealing proves the first player wins (Chomp, Hex) without giving a strategy; it needs no draws and "an extra move never hurts".
- Pairing and mirror strategies (round table, coins in a row) give simple guarantees; check the one self-paired move, such as the centre of the table.
Practice problems
Finished reading?
Answer the 4 knowledge checks to complete this lesson (0 done).