Tech Interview

Hard River Crossing

a disfunctional family has to cross the river. on one side of the river are a mom and 2 daughters, dad and 2 sons, the maid and the dog. there is a boat only big enough to hold 2 people (counting the dog as 1 person). only the adults are capable of operating the boat. everyone has to get to the other side, without anything bad happening.

difficulties: if the dog is left with anyone and the maid isn’t there to control him, he’ll bite. the dad can’t be left with any of the daughters when the mom isn’t there. likewise, the mom can’t be trusted alone with either of the sons when the dad isn’t there.

remember! only an adult can operate the boat, AND the boat can’t drive itself.

Solution

we start with a mother (m), two daughters (d1, d2), a father (f), two sons (s1, s2), a housemaid (h), and a dog (c – canine) on the west (W) shore, and they all want to get to the east (E) shore.

W = {m, d1, d2, f, s1, s2, h, c} // everyone on the west shore
E = {} // no one on the east shore

let’s move everyone, over…

housemaid and canine go east, and the housemaid comes back:

W = {m, d1, d2, f, s1, s2, h}
E = {c}

housemaid and s1 go east, h and c come back:

W = {m, d1, d2, f, s2, h, c}
E = {s1}

father and s2 go east, father comes back:

W = {m, d1, d2, f, h, c}
E = {s1, s2}

mother and father go east, mother comes back:

W = {m, d1, d2, h, c}
E = {f, s1, s2}

h and c go east, father comes back:

W = {m, d1, d2, f}
E = {s1, s2, h, c}

father and mother go east, mother comes back:

W = {m, d1, d2}
E = {f, s1, s2, h, c}

mother and d1 go east, housemaid and c come back:

W = {d2, h, c}
E = {m, d1, f, s1, s2}

h and d2 go east, h comes back

W = {h, c}
E = {m, d1, d2, f, s1, s2}

h and c go east

W = {}
E = {m, d1, d2, f, s1, s2, h, c}

done!

Exit mobile version