Lyft | USA | Phone Interview
Anonymous User
1758

Alice has a set of buckets of various sizes, and an unlimited water supply. Her task is to measure an exact amount of water in one of the buckets.

Buckets are unmarked and she only knows their full capacity, which is always an integer e.g. 5 gallons.

Alice is allowed the following operations:

  1. Fully fill a bucket.
  2. Empty a bucket.
  3. Pour bucket into another, up until the other bucket's capacity.

Note that Alice doesn't know how to measure water in any other way, e.g. she can't pour 1/2 of a bucket into another because her measurement would be inaccurate.

Example: buckets have capacity 3 and 5. Alice is asked to measure 4.
Let's denote buckets' contents by a tuple (a, b) where 0 <= a <= 3 and 0 <= b <= 5.
We start with (0, 0) and can follow these steps:

  1. Fill up the big bucket (0, 5)
  2. Pour to the smaller one (3, 2)
  3. Empty the small one (0, 2)
  4. Pour water from big to small (2, 0)
  5. Fill up the big one (2, 5)
  6. Pour water from big to small (3, 4)

0_0

Your task is to help Alice determine the shortest sequence of steps that gets desired capacity.

Here she took 6 steps to get to 4 gallons of water.

Comments (3)