Account Balance After Rounded Purchase - Problem

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).

Input & Output

Example 1 — Basic Rounding Down
$ Input: purchaseAmount = 9
Output: 90
💡 Note: 9 is closer to 10 than to 0, so it rounds to 10. Final balance: 100 - 10 = 90
Example 2 — Rounding Up with Tie
$ Input: purchaseAmount = 15
Output: 80
💡 Note: 15 is exactly between 10 and 20, so it rounds up to 20. Final balance: 100 - 20 = 80
Example 3 — No Rounding Needed
$ Input: purchaseAmount = 10
Output: 90
💡 Note: 10 is already a multiple of 10, no rounding needed. Final balance: 100 - 10 = 90

Constraints

  • 0 ≤ purchaseAmount ≤ 100

Visualization

Tap to expand
Account Balance After Rounded PurchaseInitial Balance$100Purchase Amount$23Rounded Amount$20Round to nearest 10Final Balance$100 - $20 = $80
Understanding the Visualization
1
Input
Purchase amount and initial balance of 100
2
Round
Round purchase to nearest multiple of 10 (5s round up)
3
Subtract
Subtract rounded amount from balance
Key Takeaway
🎯 Key Insight: Add 5 then integer divide by 10 and multiply back - this naturally handles the upward rounding rule
Asked in
Google 12 Amazon 8
15.0K Views
Medium Frequency
~5 min Avg. Time
450 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