Return Cash-Register Change in Named Denominations
Bank of AmericaSoftware Engineer · Online AssessmentLast updated
Implement a cash register function that calculates change in standard currency denominations using integer arithmetic and returns the dispensed items sorted alphabetically.
Problem Statement
Design a function that computes the exact change to return to a customer given the purchase price and the cash tendered. Both values are provided as decimal strings.
To avoid floating-point precision issues, all monetary calculations must be performed using integer arithmetic (e.g., converting dollars and cents into total cents).
Use the following standard currency denominations:
| Denomination | Value in Cents |
|---|---|
PENNY | 1 |
NICKEL | 5 |
DIME | 10 |
QUARTER | 25 |
HALF DOLLAR | 50 |
ONE | 100 |
TWO | 200 |
FIVE | 500 |
TEN | 1000 |
TWENTY | 2000 |
FIFTY | 5000 |
ONE HUNDRED | 10000 |
Rules
- If
cash_given < price, return"ERROR". - If
cash_given == price, return"ZERO". - Otherwise, determine the minimum number of bills and coins needed using a greedy approach (taking the largest possible denomination first until the remaining balance is zero). Return a comma-separated string of the names of all dispensed items sorted in alphabetical order. If a denomination is used multiple times, it must appear in the output as many times as it was dispensed.
Function Signature
def calculate_change(price: str, cash_given: str) -> str:
pass
Input and Output
- Input: Two strings,
priceandcash_given, each formatted as a non-negative decimal with up to two decimal places (e.g.,"15.94","20","0.5"). - Output: A single string containing
"ERROR","ZERO", or a comma-separated, alphabetically sorted list of denomination names.
Constraints
0.00 <= price, cash_given <= 10000.00- Inputs contain only digits and at most one decimal point.
Examples
Example 1
calculate_change("15.94", "16.00")
# Output: "NICKEL,PENNY"
Explanation: Change is 6 cents. Greedy selection yields 1 NICKEL (5¢) and 1 PENNY (1¢). Sorted alphabetically: "NICKEL,PENNY".
Example 2
calculate_change("1.02", "1.24")
# Output: "DIME,DIME,PENNY,PENNY"
Explanation: Change is 22 cents. Greedy selection yields 2 DIME (10¢ each) and 2 PENNY (1¢ each). Sorted alphabetically: "DIME,DIME,PENNY,PENNY".
Example 3
calculate_change("50.00", "40.00")
# Output: "ERROR"
Explanation: The customer provided less money than the purchase price.
Hints
Hint 1
Parse the input strings into total cents by splitting on the decimal point rather than converting directly to floats.
Hint 2
Sort the available denominations in descending order by cent value to greedily extract the count of each denomination.
Hint 3
Collect all dispensed denomination names into a list, sort that list alphabetically, and join them with commas.
Follow-up questions
- How would your approach change if the coin system were arbitrary and greedy choice did not guarantee the minimal number of coins?
- How would you handle finite register inventory where certain denominations might be out of stock?
- How would you extend this logic to support multi-currency registers with localized denomination names and subunit rules?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.