Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: A water-jug problem: a 3-litre jug and a 5-litre jug, both empty, and unlimited water. Allowed actions: fill a jug fully, empty a jug, or pour from one into the other until the source is empty or the target is full. What is the minimum number of actions to get exactly 4 litres in the 5-litre jug?
- A. 8
- B. 6
- C. 5
- D. 3
Correct answer: B. 6
BFS over (a, b) states finds fill 5, pour 5→3 (5L has 2), empty 3, pour 5→3, fill 5, pour 5→3, leaving 4 litres in the 5-litre jug: 6 actions. Starting by filling the 3-litre jug needs 8, not counting the 'empty' action gives 5, and counting only the pours gives 3.
More Coding Interview Problems questions
- Problem: The string "PAYPALISHIRING" is written in a zigzag on 3 rows (letters go down the rows in a zigzag, then the rows are read left…
- Problem: How many ordered pairs (i, j) with 1 ≤ i < j ≤ 10 satisfy i XOR j = 3?
- Problem: In how many ways can 100 be written as a sum of two perfect squares x² + y² with 0 ≤ x ≤ y (unordered, non-negative)?
- Problem: How many prime numbers are there in the range 1 to 200 (inclusive)?
- Problem: Compute σ(360), the sum of all positive divisors of 360.
- Problem: The gas-station problem: gas = [1, 2, 3, 4, 5] and cost = [3, 4, 5, 1, 2] around a circular route of 5 stations, starting with an…