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.

Your win todayDecide instantly whether a jug puzzle is solvable, and if it is, solve it with a step table, out loud.

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:

Target T is measurable ⇔ T is a multiple of gcd(A, B) and T ≤ the bigger jug
(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])
8 L & 4 Lgcd = 4 5 L & 3 Lgcd = 1 0 1 2 3 4 5 ✗ 6 7 8 0 1 2 3 4 ✓ 5
Reachable amounts are big dots. With 8 & 4 you can only ever hold 0, 4 or 8 litres in a jug, so 5 is out of reach. With 5 & 3 (gcd 1), every whole number up to 5 is reachable.
Say it in the interview (for 8 L & 4 L → 5 L)"It's impossible. Every fill, empty or pour moves water in multiples of 4 litres, which is gcd(8, 4). So each jug only ever holds 0, 4 or 8 litres. If the question was meant to be 8 and 3, then fill the 8 and pour it into the 3, and 5 litres remain."

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

  1. 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.
  2. Write a state table (A, B) after every move, so you never lose track.
  3. 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)

(0, 0)start (5, 0)fill 5 (2, 3)5 → 3 (2, 0)empty 3 (0, 2)5 → 3 (5, 2)fill 5 (4, 3) ✓5 → 3 (1 fits) Pattern: fill the big jug, pour into the small one, empty the small one, repeat
Six moves. Each "5 → 3" pour stops when the 3 L jug is full or the 5 L jug is empty, whichever comes first.

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
#Move8 L5 L3 L
0start800
18 → 5350
25 → 3323
33 → 8620
45 → 3602
58 → 5152
65 → 3143
73 → 8440

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

Go deeper

Primary source: raw.org: Solution to the water jug problem. It covers the gcd/Bézout proof of which amounts are measurable. Practice: IndiaBIX Logical Games.

Related: Lesson 10: AP & trains · Aptitude & GD cheat sheet · Aptitude question bank

💬 Ask your teacher: "give me 5 more brain teasers like this" (hourglasses, coins, weighing).