Logic · Brainteasers
Water-jug puzzles: a rule, a method, a simulator
The example from your interview brief, "use an 8 L and a 4 L can to get 5 L", is impossible. Proving that in ten seconds will impress more than any fiddling. Here's why, and how to solve the ones that are possible.
Step 0: the GCD test (always do this first)
You can fill, empty, and pour one jug into the other until the source is empty or the target is full. Every one of these moves keeps each jug's contents a multiple of gcd(A, B). So:
(This is Bézout's identity: every integer combination mA + nB is a multiple of the gcd, and the gcd itself is reachable.[raw.org])
Interviewers sometimes give an impossible puzzle on purpose to see whether you'll reason or just flail. Also: if they actually said 8 L, 5 L and 3 L, that's the three-jug classic below.
The method for solvable puzzles
- Pick a direction and stick to it: either keep filling the small jug and pouring it into the big one, or keep filling the big jug and pouring it into the small one. Empty the receiving jug when it's full.
- Write a state table (A, B) after every move, so you never lose track.
- Stop when either jug shows the target. If one direction is long, try the other.
Worked: 5 L & 3 L → 4 L (the "Die Hard" puzzle)
Play: jug simulator
Three jugs: split 8 L into 4 + 4 using 5 L and 3 L
Start with (8, 0, 0): the 8 L jug full, the others empty. Try it on paper first.
Show the 7-pour solution
| # | Move | 8 L | 5 L | 3 L |
|---|---|---|---|---|
| 0 | start | 8 | 0 | 0 |
| 1 | 8 → 5 | 3 | 5 | 0 |
| 2 | 5 → 3 | 3 | 2 | 3 |
| 3 | 3 → 8 | 6 | 2 | 0 |
| 4 | 5 → 3 | 6 | 0 | 2 |
| 5 | 8 → 5 | 1 | 5 | 2 |
| 6 | 5 → 3 | 1 | 4 | 3 |
| 7 | 3 → 8 | 4 | 4 | 0 |
Retrieval check
Can you get 5 L from an 8 L and a 4 L can?
With 9 L and 6 L jugs, which target is impossible?
8 L & 3 L → 5 L. What's the first move?
5 L & 3 L: after "fill 5, pour into 3", what is (5 L, 3 L)?
Type it
10 L & 6 L jugs. Can you measure exactly 7 L? (yes / no)
Your turn
- Solve every simulator preset. Aim to match the optimal step count it reports.
- On the 8 L & 4 L preset, click "Is it possible?", then explain the reason out loud.
- Solve the three-jug 8-5-3 split on paper before opening the solution.
- Make up your own pair (e.g. 11 L & 7 L) and predict solvability with the GCD rule.
- Practise narrating a solution as a state table, the way you would at a whiteboard.
Go deeper
Related: Lesson 10: AP & trains · Aptitude & GD cheat sheet · Aptitude question bank