Magicsheet logo

Pour Water Between Buckets to Make Water Levels Equal

Medium
89.1%
Updated 6/1/2025

Asked by 1 Company

Pour Water Between Buckets to Make Water Levels Equal

What is this problem about?

The Pour Water Between Buckets to Make Water Levels Equal problem gives you buckets with different water amounts and a loss percentage per pour. Find the maximum equal water level achievable. This coding problem uses binary search on the answer (the target level) with a feasibility check. The array and binary search interview pattern is demonstrated.

Why is this asked in interviews?

Deutsche Bank asks this to test binary search on the answer with a custom feasibility check. The monotonic property: if level L is achievable, any level L' < L is also achievable. Binary search on the decimal level value and verify if total gained water (from pouring excess buckets) can fill all below-L buckets given the loss.

Algorithmic pattern used

Binary search on target level. For a given level L: water gained from buckets above L = sum(bucket - L for bucket > L) multiplied by (100 - loss) / 100. Water needed to fill below-L buckets = sum(L - bucket for bucket < L). If gained >= needed, L is feasible. Binary search for the maximum feasible L with precision (e.g., 1e-5 iterations or 100 iterations of floating-point binary search).

Example explanation

buckets=[1,2,7], loss=80. L=3: gain from 7=(7-3)*0.2=0.8. Need for 1,2: (3-1)+(3-2)=3. 0.8<3, not feasible. L=2: gain=(7-2)*0.2=1. Need=(2-1)=1. 1>=1 ✓. L=2 is achievable. Answer ≈ 2.00000.

Common mistakes candidates make

  • Using integer binary search (levels are floating-point).
  • Incorrect loss computation (gain = excess * (1 - loss/100)).
  • Off-by-one in convergence criteria for floating-point binary search.
  • Forgetting that only buckets above L contribute gain.

Interview preparation tip

Floating-point binary search problems iterate ~100 times with mid = (lo + hi) / 2 until convergence. The feasibility check defines the monotone condition. Practice floating-point binary search on: "minimum time," "maximum efficiency," "optimal allocation" problems where the answer is a real number. The convergence criterion (100 iterations or error < 1e-6) is standard.

Similar Questions