Simulate Account Transactions with Integer Cashbacks
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Process a series of deposits, conditional withdrawals, and integer floor cashbacks to compute a final account balance.
You are tasked with maintaining a ledger for an account balance. Starting with an initial integer balance, you must process a sequence of transactions in the exact order they are given and return the resulting balance.
Each transaction is represented as a string containing an operation type followed by a non-negative integer parameter space-separated:
"DEPOSIT x": Increase the current balance byx."WITHDRAW x": Decrease the current balance byxonly if the current balance is at leastx. If the balance is less thanx, the transaction is skipped."CASHBACK p": Increase the current balance byfloor(balance * p / 100).
Constraints
0 <= balance <= 10^180 <= len(operations) <= 10^5- For each operation parameter
xorp:0 <= x, p <= 10^9. - All operations follow the exact format
"<TYPE> <VALUE>".
Examples
Example 1
Input: process_bank_account(100, ['DEPOSIT 50', 'WITHDRAW 120', 'CASHBACK 10', 'WITHDRAW 15'])
Output: 18
Hints
Hint 1
Iterate through the array of transaction strings step-by-step to maintain a running balance.
Hint 2
Ensure withdrawals are only executed if the current balance is greater than or equal to the requested amount.
Hint 3
Use exact integer arithmetic for cashback calculations (`balance * p // 100`) rather than floating-point math to prevent precision errors.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires a straightforward simulation of sequential account transactions.
We maintain a single state variable, balance, initialized with the starting balance. We then parse and process each command string sequentially:
- Splitting the Command: Each entry consists of an operation name and an integer argument separated by a space.
- Handling Operations:
DEPOSIT x: Simply addxtobalance.WITHDRAW x: Check ifbalance >= x. If true, subtractx; otherwise, make no changes.CASHBACK p: Calculate the cashback bonus asfloor(balance * p / 100). Using standard integer division (balance * p // 100) guarantees exact computation without floating-point inaccuracies.
Because every transaction relies on the output of the preceding transaction, the operations must be processed strictly sequentially in $O(N)$ time.
Complexity
- Time Complexity: $\mathcal{O}(N)$, where $N$ is the number of operations. Each string operation involves constant-time parsing and basic integer arithmetic.
- Space Complexity: $\mathcal{O}(1)$ auxiliary space beyond the inputs, as we only need to update the balance in place.
from typing import List
def process_bank_account(balance: int, operations: List[str]) -> int:
for operation in operations:
kind, amount_text = operation.split()
amount = int(amount_text)
if kind == "DEPOSIT":
balance += amount
elif kind == "WITHDRAW":
if balance >= amount:
balance -= amount
elif kind == "CASHBACK":
balance += balance * amount // 100
return balance