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.
Initially, you have a bank account balance of 100 dollars.
You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars, in other words, its price.
When making the purchase, first the purchaseAmount is rounded to the nearest multiple of 10. Let us call this value roundedAmount. Then, roundedAmount dollars are removed from your bank account.
Return an integer denoting your final bank account balance after this purchase.
Notes:
Example 1:
Input: purchaseAmount = 9
Output: 90
Explanation:
The nearest multiple of 10 to 9 is 10. So your account balance becomes 100 - 10 = 90.
Example 2:
Input: purchaseAmount = 15
Output: 80
Explanation:
The nearest multiple of 10 to 15 is 20. So your account balance becomes 100 - 20 = 80.
Example 3:
Input: purchaseAmount = 10
Output: 90
Explanation:
10 is a multiple of 10 itself. So your account balance becomes 100 - 10 = 90.
Constraints:
0 <= purchaseAmount <= 100Problem summary: Initially, you have a bank account balance of 100 dollars. You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars, in other words, its price. When making the purchase, first the purchaseAmount is rounded to the nearest multiple of 10. Let us call this value roundedAmount. Then, roundedAmount dollars are removed from your bank account. Return an integer denoting your final bank account balance after this purchase. Notes: 0 is considered to be a multiple of 10 in this problem. When rounding, 5 is rounded upward (5 is rounded to 10, 15 is rounded to 20, 25 to 30, and so on).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
9
15
10
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2806: Account Balance After Rounded Purchase
class Solution {
public int accountBalanceAfterPurchase(int purchaseAmount) {
int diff = 100, x = 0;
for (int y = 100; y >= 0; y -= 10) {
int t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
}
// Accepted solution for LeetCode #2806: Account Balance After Rounded Purchase
func accountBalanceAfterPurchase(purchaseAmount int) int {
diff, x := 100, 0
for y := 100; y >= 0; y -= 10 {
t := abs(y - purchaseAmount)
if t < diff {
diff = t
x = y
}
}
return 100 - x
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2806: Account Balance After Rounded Purchase
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
diff, x = 100, 0
for y in range(100, -1, -10):
if (t := abs(y - purchaseAmount)) < diff:
diff = t
x = y
return 100 - x
// Accepted solution for LeetCode #2806: Account Balance After Rounded Purchase
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2806: Account Balance After Rounded Purchase
// class Solution {
// public int accountBalanceAfterPurchase(int purchaseAmount) {
// int diff = 100, x = 0;
// for (int y = 100; y >= 0; y -= 10) {
// int t = Math.abs(y - purchaseAmount);
// if (t < diff) {
// diff = t;
// x = y;
// }
// }
// return 100 - x;
// }
// }
// Accepted solution for LeetCode #2806: Account Balance After Rounded Purchase
function accountBalanceAfterPurchase(purchaseAmount: number): number {
let [diff, x] = [100, 0];
for (let y = 100; y >= 0; y -= 10) {
const t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
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.