Measuring & Pouring Puzzles โ Revision Notes
The water-jug family ("measure 4 litres with a 5L and 3L jug") tests systematic search and arithmetic reasoning. The trick is to treat it as reachable combinations rather than guessing โ any amount that's a multiple of the GCD of the jug sizes is achievable.
The method
- Operations: fill a jug, empty a jug, pour one into another until the source empties or the target fills.
- Solvability: target T is reachable iff T is a multiple of gcd(a, b) and T โค max(a, b).
- Strategy: repeatedly fill one jug and pour into the other, tracking the sequence โ it cycles through all reachable amounts.
Example (5L, 3L โ 4L): fill 5, pour into 3 (leaves 2), empty 3, pour the 2 in, fill 5, top up the 3 (uses 1) โ 4 left in the 5L jug.
Exam Tricks & Tips
- ๐ฏ Check gcd first โ if the target isn't a multiple of gcd(jug sizes), it's impossible; say so immediately.
- ๐ฏ Pick one "fill" jug and pour into the other systematically โ the sequence enumerates all reachable amounts.
- ๐ฏ Track state as (jug A, jug B) pairs so you never loop or lose your place.
- ๐ฏ Work backwards from the target sometimes โ it can be shorter.
- ๐ฏ Narrate each fill/empty/pour โ the interviewer follows your systematic method.
- โ Common mistake: random guessing of pours with no state-tracking โ you loop endlessly and can't explain it.
Expected pattern: A staple reasoning puzzle; interviewers watch for a systematic method and the gcd insight.
Quick recap: Reachable iff target is a multiple of gcd(a,b). Fill-and-pour systematically, track (A,B) state, narrate. Guessing loops forever.
Measuring & Pouring Puzzles โ Flashcards (Interview Prep)
Cover the answer, recall, then check. 10 cards on measuring & pouring puzzles.
Q1. When is a target amount reachable with two jugs?
A1. When it is a multiple of gcd(jug sizes) and does not exceed the larger jug.
Q2. First thing to check in a jug puzzle?
A2. The gcd โ if the target is not a multiple of gcd(a,b), it is impossible.
Q3. The three operations allowed?
A3. Fill a jug, empty a jug, and pour one into another until source empties or target fills.
Q4. Systematic strategy for jug puzzles?
A4. Repeatedly fill one jug and pour into the other; the sequence enumerates all reachable amounts.
Q5. How do you avoid looping?
A5. Track state as (jug A, jug B) pairs so you never revisit or lose your place.
Q6. Measure 4L with a 5L and 3L jug โ outline.
A6. Fill 5โpour into 3 (2 left)โempty 3โpour 2 inโfill 5โtop up 3 (uses 1)โ4 left in the 5L.
Q7. Why can working backwards help?
A7. Reasoning from the target amount is sometimes a shorter path.
Q8. Common jug-puzzle mistake?
A8. Random guessing of pours with no state-tracking โ you loop endlessly.
Q9. What should you do while solving aloud?
A9. Narrate each fill/empty/pour so the interviewer follows your method.
Q10. If gcd(a,b)=1, which targets are reachable?
A10. Every integer amount up to max(a,b), since 1 divides everything.
Measuring & Pouring Puzzles
"You have a 3-litre and a 5-litre jug โ measure exactly 4 litres." Water-jug and measuring puzzles are interview staples because they have a clean, teachable method and reward systematic search over guessing. Once you see them as a state-space you can traverse with a few fixed operations, they stop being tricky and become almost mechanical.
What this covers / why it matters: the technique for jug/measuring puzzles โ the allowed operations and how to search states โ with a fully worked classic. It's a recurring, learnable puzzle type.
The approach
Beginner โ the allowed operations
With two jugs there are only ever a handful of moves, and every solution is a sequence of them:
- Fill a jug completely (from the tap).
- Empty a jug completely.
- Pour one jug into the other until either the source is empty or the destination is full.
A puzzle "state" is the pair (amount in jug A, amount in jug B). You start at (0, 0) and search for a state containing the target amount.
Intermediate โ the systematic strategy
Rather than guessing, pick one jug to repeatedly fill and pour into the other; the remainders you're forced to leave behind generate the measurable amounts. Two reliable strategies:
- Fill-big, pour-into-small: keep filling the large jug and emptying it into the small one, emptying the small jug when full โ the large jug's leftover cycles through useful amounts.
- Fill-small, pour-into-big: the mirror image.
Try one direction; if it doesn't reach the target, try the other.
Advanced โ the math behind it
You can measure a target T with jugs of size a and b if and only if T is a multiple of gcd(a, b) and T is at most max(a, b). For 3 and 5, gcd is 1, so every whole number up to 5 is measurable โ that's why these puzzles are solvable. Knowing this lets you immediately tell an interviewer whether a target is even possible (e.g. with 4- and 6-litre jugs, gcd is 2, so you can never measure an odd amount like 5).
Worked example
"Measure exactly 4 litres using a 3-litre and a 5-litre jug."
Tracking states as (3-jug, 5-jug), using fill-big-pour-into-small:
- Fill the 5-jug: (0, 5).
- Pour 5 into 3 until 3 is full: (3, 2). โ 3 litres moved, 2 left in the big jug.
- Empty the 3-jug: (0, 2).
- Pour the 5-jug's 2 litres into the 3-jug: (2, 0).
- Fill the 5-jug again: (2, 5).
- Pour 5 into 3 until 3 is full โ the 3-jug already has 2, so it takes just 1 more: (3, 4).
The 5-litre jug now holds exactly 4 litres. Solved in six moves.
"I searched by always filling the big jug and topping up the small one; the amount stranded in the big jug walked through 2, then 4 โ the target. Since gcd(3,5) = 1, I knew 4 had to be reachable."
What interviewers look for
- A systematic search, not random pouring.
- Clear state tracking โ you can say the (A, B) amounts at each step.
- The gcd insight (bonus) โ knowing when a target is possible at all.
- Narration of why each pour is made.
Do's and don'ts
- Do track states as (jug A, jug B) and apply the fill/empty/pour operations.
- Do mention the gcd rule to judge solvability.
- Don't pour randomly hoping to stumble on the answer.
- Don't forget you can pour either direction and empty jugs.
- Mnemonic: FEP โ Fill, Empty, Pour โ the only three moves; track the state pair.
Pouring haphazardly without tracking the state of each jug, then losing the thread and starting over. These puzzles are a small, finite state search โ write the (A, B) amounts after every move and commit to one strategy (e.g. fill-big, pour-into-small). Systematic beats lucky, and you can always narrate exactly where you are.
- โ- Only three operations exist: fill, empty, pour-until-source-empty-or-dest-full.
- โ- Track each state as (jug A amount, jug B amount) and search from (0, 0) to the target.
- โ- A committed strategy โ fill one jug, pour into the other, empty as needed โ reaches the answer systematically.
- โ- Solvable iff the target is a multiple of gcd(a, b) and at most the larger jug; gcd(3,5)=1 makes all amounts reachable.
- โJug puzzles are a finite state search with three moves. Track amounts as (A, B), commit to filling one jug and pouring into the other, and use the gcd rule to know instantly whether a target is even possible.
Measuring & Pouring Puzzles โ Worked Example
Worked Example
Problem/Question: You have a 3-litre jug and a 5-litre jug and an unlimited water supply. Measure exactly 4 litres.
Solution/Model answer: Work by filling, emptying and transferring between jugs; track the state as (3L jug, 5L jug).
- Fill the 5L jug: state (0, 5).
- Pour 5L into 3L until the 3L is full (moves 3L): state (3, 2). The 5L now holds 2.
- Empty the 3L jug: state (0, 2).
- Pour the 2L from the 5L into the 3L: state (2, 0).
- Fill the 5L jug again: state (2, 5).
- Pour from 5L into 3L until the 3L is full โ the 3L needs only 1 more litre (it has 2), so 1L moves: state (3, 4).
The 5L jug now holds exactly 4 litres. (An alternative path fills the 3L twice into the 5L.)
Answer/Takeaway: Reach 4 litres by filling the 5L, topping up the 3L (leaving 2 in the 5L), emptying the 3L, transferring the 2, refilling the 5L, and topping the 3L again โ leaving exactly 4 in the 5L. The method: track (small, large) states and use fill/empty/transfer operations systematically.
- โ- Represent the puzzle as states (jugA, jugB) and apply three moves: fill, empty, transfer.
- โ- Search systematically rather than guessing; the "leave a remainder, then top up" idea is the key trick.
- โ- Narrate each state transition โ clear, systematic reasoning is what the interviewer scores.