Magicsheet logo

Water Bottles

Easy
70.8%
Updated 6/1/2025

Water Bottles

What is this problem about?

The Water Bottles coding problem is a fun simulation task. You start with a certain number of full water bottles. After drinking one, it becomes an empty bottle. You can exchange a specific number of empty bottles for one full bottle. The question asks for the maximum number of water bottles you can drink.

Why is this asked in interviews?

This is a popular Easy-level question for Microsoft and Amazon because it tests basic Simulation and Loop logic. It requires the candidate to keep track of two changing variables: the current number of full bottles and the current number of empty bottles. It’s a great way to check if a candidate can translate a simple word problem into efficient code.

Algorithmic pattern used

This follows the Simulation interview pattern. You use a while loop that continues as long as you have enough empty bottles to make an exchange. In each iteration, you "drink" the full bottles (add to total), count the resulting empty bottles, and then perform the exchange to get new full bottles.

Example explanation

Suppose you have 9 full bottles and the exchange rate is 3 empty bottles for 1 full.

  1. Drink 9 bottles. Total = 9. Empty = 9.
  2. Exchange 9 empty for 3 full.
  3. Drink 3 bottles. Total = 9 + 3 = 12. Empty = 3.
  4. Exchange 3 empty for 1 full.
  5. Drink 1 bottle. Total = 12 + 1 = 13. Empty = 1. You can't exchange anymore. Final total = 13.

Common mistakes candidates make

A common error is forgetting to carry over the "leftover" empty bottles that weren't used in an exchange. For example, if you have 10 empty bottles and exchange 3 for 1, you have 1 new full bottle AND 1 leftover empty bottle. Not accounting for these leftovers will lead to an incorrect answer.

Interview preparation tip

When writing simulations, be very careful with variable updates. Ensure that you update your "empty bottles" count after you've calculated how many new full bottles you get, but before you start the next round of drinking.

Similar Questions