KatInterview

Tank Filling: Can You Hit an Exact Volume?

Math & Bit ManipulationAuto-graded · 22 test cases
MediumAuto-gradedMath & Bit Manipulationmathbfsnumber-theory

Last updated

Tests whether a candidate can apply GCD-based number theory and state-space reasoning to determine if a target volume is reachable with two containers.

Tank Filling: Can You Hit an Exact Volume?

You have two tanks with fixed capacities x liters and y liters, plus an unlimited water supply. At any point you may perform exactly one of the following operations:

  • Fill a tank completely from the supply.
  • Drain a tank completely onto the ground.
  • Transfer water from one tank into the other until the source tank is empty or the destination tank is full, whichever comes first.

You start with both tanks empty. Determine whether some sequence of these operations can leave exactly z liters in one of the two tanks.

Return True if that target is reachable, False otherwise.


Approach guidance

You are expected to reason about two complementary approaches:

State-space search (BFS/DFS)

Model each situation as a pair (a, b) representing the current contents of each tank (0 ≤ a ≤ x, 0 ≤ b ≤ y). Starting from (0, 0), apply all valid operations as edges and search for any state where a == z or b == z.

  • How many distinct states exist?
  • What is the time and space complexity of this search?
  • When does this approach become impractical?

Number-theoretic shortcut

There is a closed-form condition using the greatest common divisor of x and y. Derive it by identifying an invariant that every reachable volume satisfies, then argue (using Bézout's identity) that every value satisfying the invariant is in fact reachable.

  • What is the exact condition on z?
  • What is the time and space complexity of this approach?

Edge cases to address

  • z == 0: both tanks start empty, so this is always achievable.
  • z > max(x, y): a single tank can never hold more than its capacity.
  • x == 0 or y == 0: one tank is useless; only 0 and the other tank's capacity are reachable.
  • x == 0 and y == 0: nothing can ever be stored; only z == 0 is achievable.

Constraints

  • 0 ≤ x, y, z ≤ 10^9
  • All three values are non-negative integers.
  • "Exactly z liters" means one tank holds precisely z, not that the two tanks together total z.
  • The operations may be applied in any order, any number of times.

Examples

Example 1

Input: can_measure(3, 5, 4)

Output: True

Example 2

Input: can_measure(2, 6, 5)

Output: False

Hints

Hint 1

Start by asking: what property is shared by every volume that can ever appear in a tank? Notice that every fill sets a tank to x or y, every drain sets it to 0, and every transfer moves an amount bounded by the smaller of what's available and what fits — all multiples of gcd(x, y).

Hint 2

Once you have the invariant, argue the other direction: for any multiple of g = gcd(x, y) that fits in the larger tank, you can actually reach it. Think about repeatedly filling the small tank and pouring it into the large one, emptying the large tank whenever it overflows. How many distinct residues does this cycle through?

Hint 3

Handle the degenerate cases before touching modulo arithmetic. Check z == 0 first (always true), then z > max(x, y) (always false, and this also sidesteps division by zero when both capacities are 0).