Account Balance After Rounded Purchase

Easy
559.

Account Balance After Rounded Purchase

Account Balance After Rounded Purchase is an easy coding problem on math, asked in Amazon and Infosys interviews.

You start with a balance of 100. A purchase of purchaseAmount is charged after being rounded to the nearest multiple of 10; a tie rounds up (so 15 becomes 20).

Return the balance after the purchase.

Example 1

Input
purchaseAmount = 9
Output
90
Explanation
9 rounds to 10, leaving 100 - 10 = 90.

Example 2

Input
purchaseAmount = 15
Output
80
Explanation
A tie rounds up to 20.

Example 3

Input
purchaseAmount = 100
Output
0

Constraints

  • 0 <= purchaseAmount <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} purchaseAmount
 * @return {number}
 */
var accountBalanceAfterPurchase = function(purchaseAmount) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Account Balance After Rounded Purchase — Problem & Solution

Account Balance After Rounded Purchase solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Account Balance After Rounded Purchase is an easy coding problem on math, asked in Amazon and Infosys interviews.

  • Difficulty: Easy
  • Topics: Math
  • Asked at: Amazon, Infosys
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You start with a balance of 100. A purchase of purchaseAmount is charged after being rounded to the nearest multiple of 10; a tie rounds up (so 15 becomes 20).

Return the balance after the purchase.

Example 1

Input: purchaseAmount = 9
Output: 90
Explanation: 9 rounds to 10, leaving 100 - 10 = 90.

Example 2

Input: purchaseAmount = 15
Output: 80
Explanation: A tie rounds up to 20.

Example 3

Input: purchaseAmount = 100
Output: 0

Constraints

  • 0 <= purchaseAmount <= 100

Hints for Account Balance After Rounded Purchase

  1. Hint 1

    Look at purchaseAmount % 10 to decide the direction.

  2. Hint 2

    A remainder of 5 or more rounds up; anything less rounds down.

  3. Hint 3

    Subtract the rounded amount from 100.

How to solve Account Balance After Rounded Purchase

Rounding to the nearest ten is decided entirely by the units digit, with the tie rule fixed to round up.

Approach

  1. Compute rem = purchaseAmount % 10.
  2. If rem >= 5, the rounded amount is purchaseAmount - rem + 10; otherwise it is purchaseAmount - rem.
  3. Return 100 - rounded.

Why it works

purchaseAmount - rem is the multiple of ten immediately below (or equal), and adding 10 moves to the one above. The >= 5 test implements "nearest, ties up" exactly.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • Language rounding helpers often use banker's rounding, which sends 15 to 10 rather than 20.
  • purchaseAmount = 0 rounds to 0 and leaves the balance at 100.
  • The maximum charge is 100, so the balance never goes negative.

Reference solution

Python

def accountBalanceAfterPurchase(purchaseAmount: int) -> int:
    rem = purchaseAmount % 10
    rounded = purchaseAmount - rem + (10 if rem >= 5 else 0)
    return 100 - rounded

Java

public static int accountBalanceAfterPurchase(int purchaseAmount) {
    int rem = purchaseAmount % 10;
    int rounded = rem >= 5 ? purchaseAmount - rem + 10 : purchaseAmount - rem;
    return 100 - rounded;
}

C++

int accountBalanceAfterPurchase(int purchaseAmount) {
    int rem = purchaseAmount % 10;
    int rounded = rem >= 5 ? purchaseAmount - rem + 10 : purchaseAmount - rem;
    return 100 - rounded;
}

JavaScript

var accountBalanceAfterPurchase = function(purchaseAmount) {
    const rem = purchaseAmount % 10;
    const rounded = rem >= 5 ? purchaseAmount - rem + 10 : purchaseAmount - rem;
    return 100 - rounded;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue