Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.
The operation of drinking a full water bottle turns it into an empty bottle.
Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.
Example 1:
Input: numBottles = 9, numExchange = 3 Output: 13 Explanation: You can exchange 3 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 9 + 3 + 1 = 13.
Example 2:
Input: numBottles = 15, numExchange = 4 Output: 19 Explanation: You can exchange 4 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 15 + 3 + 1 = 19.
Constraints:
1 <= numBottles <= 1002 <= numExchange <= 100Problem summary: There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle. The operation of drinking a full water bottle turns it into an empty bottle. Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
9 3
15 4
water-bottles-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1518: Water Bottles
class Solution {
public int numWaterBottles(int numBottles, int numExchange) {
int ans = numBottles;
for (; numBottles >= numExchange; ++ans) {
numBottles -= (numExchange - 1);
}
return ans;
}
}
// Accepted solution for LeetCode #1518: Water Bottles
func numWaterBottles(numBottles int, numExchange int) int {
ans := numBottles
for ; numBottles >= numExchange; ans++ {
numBottles -= (numExchange - 1)
}
return ans
}
# Accepted solution for LeetCode #1518: Water Bottles
class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
ans = numBottles
while numBottles >= numExchange:
numBottles -= numExchange - 1
ans += 1
return ans
// Accepted solution for LeetCode #1518: Water Bottles
struct Solution;
impl Solution {
fn num_water_bottles(num_bottles: i32, num_exchange: i32) -> i32 {
let mut full = num_bottles;
let mut empty = 0;
let mut res = 0;
while full > 0 {
res += full;
empty += full;
full = empty / num_exchange;
empty %= num_exchange;
}
res
}
}
#[test]
fn test() {
let num_bottles = 9;
let num_exchange = 3;
let res = 13;
assert_eq!(Solution::num_water_bottles(num_bottles, num_exchange), res);
let num_bottles = 15;
let num_exchange = 4;
let res = 19;
assert_eq!(Solution::num_water_bottles(num_bottles, num_exchange), res);
let num_bottles = 15;
let num_exchange = 4;
let res = 19;
assert_eq!(Solution::num_water_bottles(num_bottles, num_exchange), res);
let num_bottles = 5;
let num_exchange = 5;
let res = 6;
assert_eq!(Solution::num_water_bottles(num_bottles, num_exchange), res);
let num_bottles = 2;
let num_exchange = 3;
let res = 2;
assert_eq!(Solution::num_water_bottles(num_bottles, num_exchange), res);
}
// Accepted solution for LeetCode #1518: Water Bottles
function numWaterBottles(numBottles: number, numExchange: number): number {
let ans = numBottles;
for (; numBottles >= numExchange; ++ans) {
numBottles -= numExchange - 1;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.