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.
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.
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.
Suppose you have 9 full bottles and the exchange rate is 3 empty bottles for 1 full.
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.
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.