Account Balance After Rounded Purchase - Problem
Account Balance After Rounded Purchase

You're managing your bank account and need to calculate your remaining balance after making a purchase. Here's the catch: your bank has a special rounding policy for purchases!

The Scenario:
• You start with exactly $100 in your account
• When you make a purchase, the bank rounds the amount to the nearest multiple of 10
• The rounded amount (not the original) is deducted from your balance

Rounding Rules:
• Round to the nearest multiple of 10 (0, 10, 20, 30, ...)
• When the last digit is 5, round UP (15 → 20, 25 → 30)
• 0 is considered a multiple of 10

Your Task:
Given a purchaseAmount, calculate and return your final account balance after the purchase.

Example: If you want to buy something for $23, it rounds to $20, so your balance becomes $100 - $20 = $80.

Input & Output

example_1.py — Python
$ Input: purchaseAmount = 15
Output: 80
💡 Note: 15 rounds to 20 (when the last digit is 5, we round up). Balance becomes 100 - 20 = 80.
example_2.py — Python
$ Input: purchaseAmount = 23
Output: 80
💡 Note: 23 is closer to 20 than to 30 (distance 3 vs 7), so it rounds to 20. Balance becomes 100 - 20 = 80.
example_3.py — Python
$ Input: purchaseAmount = 9
Output: 90
💡 Note: 9 is closer to 10 than to 0 (distance 1 vs 9), so it rounds to 10. Balance becomes 100 - 10 = 90.

Visualization

Tap to expand
Bank Account Rounding ProcessStarting Balance$100Purchase Amount$23Rounded Amount$20Rounding Process$10Distance: 13$20Distance: 3 ✓$30Distance: 7$23Final Balance$100 - $20 = $80Closest multiple of $10 to $23 is $20
Understanding the Visualization
1
Original Purchase
You want to buy something for $23
2
Find Closest Multiple
Bank rounds $23 to nearest $10: either $20 or $30
3
Calculate Distance
|23-20| = 3, |23-30| = 7, so choose $20
4
Deduct from Balance
Final balance = $100 - $20 = $80
Key Takeaway
🎯 Key Insight: The mathematical formula (amount + 5) // 10 * 10 automatically handles all rounding cases including the "round up on 5" rule.

Time & Space Complexity

Time Complexity
⏱️
O(n)

We might iterate up to purchaseAmount/10 times in worst case

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables regardless of input size

n
2n
Linear Space

Constraints

  • 0 ≤ purchaseAmount ≤ 100
  • The initial account balance is always $100
  • Rounding rule: When the last digit is 5, always round UP
Asked in
Apple 15 Google 12 Meta 8 Amazon 6
28.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen