FastPrepThree- and Five-liter Jug Plan
Problems/Three- and Five-liter Jug Plan

Three- and Five-liter Jug Plan

Xilinx logoXilinx● EasyNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks how to measure4 liters with3-liter and5-liter cups. It does not ask for minimum steps or specify output. This practice returns a concrete action-state plan, generalizes the target to0..5, and fixes a5-to3 pouring policy solely to make the output deterministic. Approximate source match: 75%.

You have a3-liter jug and a5-liter jug, both initially empty, plus unlimited water and a drain. Construct a sequence of jug volumes that leaves exactly target liters in the5-liter jug. The reported interview asks for4 liters; this practice also includes the other integer targets from0 through5.

Return rows [amountIn3LiterJug,amountIn5LiterJug], including the initial state and the state after every action. To select one reproducible plan, repeat this policy until the5-liter amount equals target:

  1. If the5-liter jug is empty, fill it completely.
  2. Otherwise, if the3-liter jug is full, empty it completely.
  3. Otherwise, pour from the5-liter jug into the3-liter jug until the former is empty or the latter is full.

Stop as soon as the target is reached, including at the initial state. Use only these whole-jug fill, empty and maximal-pour actions. No minimum-step objective is imposed; the fixed policy determines the required plan.

Function

jugPlan(target: int) → int[][]

Examples

Example 1

target = 4return = [[0,0],[0,5],[3,2],[0,2],[2,0],[2,5],[3,4]]

Fill the5-liter jug, pour into the3-liter jug, empty the3-liter jug, pour the remaining2 liters into it, refill the5-liter jug, then pour1 liter. The5-liter jug now contains4 liters.

Example 2

target = 5return = [[0,0],[0,5]]

One fill of the5-liter jug reaches the target.

Example 3

target = 0return = [[0,0]]

The initial empty state already has0 liters in the5-liter jug, so no action is needed.

Constraints

  • 0 <= target <= 5.

More Xilinx problems

  • Count Ones in a 16-bit WordONSITE INTERVIEW · Seen Feb 2019
  • Delete a Given Linked-list NodeONSITE INTERVIEW · Seen Feb 2019
  • Reservation-station Tag MatchONSITE INTERVIEW · Seen Feb 2019
All Xilinx interview questionsSee Xilinx hiring insights
Saved
public int[][] jugPlan(int target) {
    return new int[0][];
}
Checking account…