Coding questions · Coinbase
Implement bank balance operations
This question evaluates object-oriented programming, state management, input validation, and arithmetic correctness when implementing account operations and maintaining transactional invariants.
Question
Design and implement a Bank account class that supports the operations Deposit(amount), Withdraw(amount), and CashBack(percent). Ensure balance updates correctly, prevent overdrafts, and return the updated balance after each operation.
Bank Balance Operations
Design a bank account that processes a sequence of operations and returns the resulting balance after each one.
You are given a list of operations. Each operation is a pair [opType, value] where opType is:
0= Depositvaluedollars into the account.1= Withdrawvaluedollars from the account.2= CashBack: addvaluepercent of the current balance back to the account (integer cashback, floor division).
The account starts with a balance of 0. Apply the operations in order and, for each one, append the resulting balance to an output list.
Validation rules (an operation that violates a rule is REJECTED — the balance is left unchanged and you append -1 for that operation):
- Deposit:
valuemust be positive (> 0). - Withdraw:
valuemust be positive and must not exceed the current balance (no overdrafts). - CashBack:
value(the percent) must be non-negative (>= 0).
Return the list of results, one entry per operation.
Example: operations [[0,100],[1,30],[2,10]] -> deposit 100 (balance 100), withdraw 30 (balance 70), cashback 10% of 70 = 7 (balance 77) -> returns [100, 70, 77].
Constraints
- 0 <= operations.length <= 10^4
- Each operation is a pair [opType, value] with opType in {0, 1, 2}
- 0 <= value <= 10^6
- Account starts at balance 0
- A rejected operation (invalid amount or overdraft) leaves the balance unchanged and yields -1
- CashBack uses integer floor division: added amount = balance * percent // 100
Hints
- Maintain a single running balance and append the new balance after each accepted operation.
- Withdraw must be rejected (append -1, leave balance unchanged) whenever the requested amount exceeds the current balance — this is how you prevent overdrafts.
- For cashback, compute balance * percent // 100 using integer (floor) division and add it to the balance.