KatInterview

Modeling Classic Puzzles: River Crossing, Jugs, and Proofs

Trees & GraphsModel answer
MediumModel answerTrees & Graphsbfsstate-space-searchinvariants

Last updated

Turn a set of contest-style logic puzzles, led by the fox-chicken-grain river crossing, into formal models, and give algorithms with proofs of correctness and optimality.

You are given a set of short, contest-style logic puzzles (roughly middle-school competition level). For each one, you must produce a method and a justification, not just a final number.

The headline puzzle:

A farmer must carry a fox, a chicken, and a sack of grain across a river. The boat holds the farmer plus at most one item. If the farmer is not present on a bank, then:

  • the fox and the chicken cannot be there together, and
  • the chicken and the grain cannot be there together.

Tasks

  1. Formalize the river crossing as a search problem. Define the state, the legal moves, which states are safe, and the goal.
  2. Give an algorithm that finds the fewest crossings and returns the sequence of moves. State its time and space complexity, including a generalization to n items with an arbitrary list of conflicting pairs.
  3. Prove that the algorithm is correct (sound, complete, optimal). Also give a short human-style argument for why the minimum is 7 crossings.
  4. Explain how you would approach other puzzles of this style. For each family below, name the technique and sketch the proof:
    • Measuring 4 L with a 3 L jug and a 5 L jug.
    • Tiling an 8×8 board with two opposite corners removed using dominoes.
    • Reaching all-heads by repeatedly flipping coins under some rule.
    • Drawing socks of 3 colors to guarantee a matching pair.
    • A game where players alternately take 1 to 3 stones and the player taking the last stone wins.
    • Finding one heavier coin among 9 using a balance scale.

State your assumptions and cover edge cases such as unsolvable instances.

Hints

Hint 1

Encode the river crossing as a tuple of four bits (farmer, fox, chicken, grain), each recording which bank the entity is on. Only 16 states exist, and several are unsafe.

Hint 2

Every crossing costs one unit, so a breadth-first search from the all-start-bank state to the all-far-bank state gives the shortest path. Store a parent pointer per state to rebuild the moves.

Hint 3

For the other puzzles, ask which tool fits: an invariant or coloring argument for impossibility, parity for toggling, pigeonhole for worst-case guarantees, backward induction for take-away games, and counting outcomes for weighing problems.