Answering without enumerating
Many interview puzzles ask "is it always possible?", "can this ever happen?" or "what is the answer, without computing?". Four tools answer most of them:
- Pigeonhole: proves something must exist.
- Symmetry: shows that outcomes come in classes of equal size, so a probability or expectation can be read off.
- Invariants: a quantity a process never changes, which proves some states are unreachable.
- Monovariants: a quantity that only moves one way, which proves a process stops.
The skill is choosing the right pigeonholes or the right invariant. Saying out loud "let me look for something that does not change" is itself a good signal in an interview.
The pigeonhole principle
If objects go into boxes, some box holds at least . Equivalently, to guarantee some box has at least objects you need of them: with one fewer, you can put exactly in every box.
The objects and boxes are rarely given. Typical choices for the boxes are remainders mod , pairs that partition a set, or labels you attach to each object.
Worked example
a block sum divisible by n
Prove that among any integers (not necessarily distinct), there is a non-empty block of consecutive ones whose sum is divisible by .
Show solutionHide solution
Take the prefix sums , , , …, . That is numbers but only possible remainders mod , so two of them agree: with . Then
a non-empty consecutive block. Including is what makes numbers enough (rather than ).
The same trick ("prefix sums, then pigeonhole the remainders") also finds a subarray with zero sum, or one with sum divisible by , in linear time with a hash map.
Choosing the pigeonholes
Pairs. Choose any numbers from . Partition the set into the pairs : two chosen numbers share a pair, so they are consecutive and hence coprime.
Odd parts. In the same setting, one chosen number divides another. Write each as with odd. There are only odd numbers below , so two chosen numbers share the same , and the one with the smaller power of 2 divides the other.
Labels (Erdős–Szekeres). Any sequence of distinct reals has an increasing or decreasing subsequence of length . Label each term with , the lengths of the longest increasing and decreasing subsequences ending at it. For , if then , otherwise , so all labels are distinct. If every there would be at most labels for terms, a contradiction.
Symmetry
Symmetry turns a probability into a counting of equal classes. Two patterns are common.
- Exchangeability: if relabelling objects does not change the distribution, they all have the same marginal behaviour. Each of the gaps that special items cut a random shuffle into has the same expected size.
- Relabelling: if objects are indistinguishable, a complicated interaction can be replaced by a simple one. When two identical ants collide and reverse, it looks exactly like them passing through each other.
Worked example
where is the first ace?
A standard 52-card deck is shuffled. What is the expected position of the first ace?
Show solutionHide solution
The 4 aces cut the 48 other cards into 5 gaps: before the first ace, between consecutive aces, and after the last. By symmetry each non-ace is equally likely to land in any of the 5 gaps. (Think of a random arrangement of 4 aces and one particular non-ace: the non-ace is equally likely to be in any of the 5 relative positions.)
By linearity, the expected number of non-aces before the first ace is , so the expected position of the first ace is
The direct computation gives the same , with a lot more work. In general, with special cards among , the first special card is at expected position .
Invariants: parity and colouring
To show a target state is unreachable, find a quantity that every allowed move preserves and that differs between the start and the target. Common invariants are parity, a sum or difference mod , and the count of cells of each colour under a well-chosen colouring.
Mutilated chessboard. Remove two opposite corners from an board. Can the 62 remaining squares be tiled by 31 dominoes? Opposite corners have the same colour, so 32 squares of one colour and 30 of the other remain. Every domino covers one square of each colour, so 31 dominoes would cover 31 of each. Impossible. (If the two removed squares have different colours, a tiling always exists; the colouring argument only proves impossibility.)
Numbers on a board. Write on a board. Repeatedly erase two numbers and write . What can the last number be? Since , the parity of the total never changes. It starts at , odd, so the final number is odd. (In fact every odd value from 1 to 9 can be reached.)
Monovariants: proving a process stops
A monovariant strictly increases (or decreases) with each move and can take only finitely many values, so the process must terminate.
Sign flips. A grid of real numbers; you may flip the sign of every entry in a row or a column. Can you always make every row sum and column sum non-negative? Repeatedly flip any line with a negative sum. Flipping a line whose sum is raises the total of the whole grid by . There are at most sign patterns, so the total takes finitely many values and cannot rise forever. When the process stops, no line has a negative sum.
The same argument proves you can split any group of people into two rooms so that everyone has at least half their friends in the other room: move anyone who violates it, and the number of friendships inside a room strictly falls.
Worked example
the chameleons
An island has 13 red, 15 green and 17 blue chameleons. When two chameleons of different colours meet, both change to the third colour. Can all 45 chameleons ever be the same colour?
Show solutionHide solution
Look at how a meeting changes the counts . A red–green meeting gives . Every pairwise difference changes by or , so each difference mod 3 is invariant.
Initially , and . All one colour means two counts are 0, so their difference is . No pair starts at , so this is impossible.
The invariant also tells you when it is possible. With 13, 16 and 16, , and in fact all 45 can become red, green or blue. A breadth-first search over the reachable states confirms both claims. In an interview, find the invariant first, then check that it is the only obstruction for the case asked.
Knowledge check
What is the smallest number of people that guarantees at least three of them were born in the same calendar month?
Knowledge check
The numbers 1 to 10 are on a board. You repeatedly erase two numbers a and b and write a + b if both are even, and |a − b| otherwise. What can you say about the last number?
Knowledge check
100 lockers are closed. Person k toggles every k-th locker, for k = 1, …, 100. How many lockers are open at the end?
Key takeaways
- Pigeonhole: objects in boxes force a box with ; objects guarantee in one box. The work is choosing the boxes: remainders, pairs, odd parts, or labels.
- Prefix sums plus pigeonhole on remainders finds a consecutive block with sum divisible by .
- Symmetry: special items split the rest into gaps of equal expected size; indistinguishable objects can be relabelled.
- Invariants (parity, sums mod , colourings) prove impossibility; monovariants over a finite state space prove termination.
- Find the invariant first, then ask whether it is the only obstruction.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).