Implement bank balance operations
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates object-oriented programming, state management, input validation, and arithmetic correctness when implementing account operations and maintaining transactional invariants.
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
Examples
Input: ([[0, 100], [1, 30], [2, 10]],)
Expected Output: [100, 70, 77]
Explanation: Deposit 100 -> 100; withdraw 30 -> 70; cashback 10% of 70 = 7 -> 77.
Input: ([[0, 50], [1, 80]],)
Expected Output: [50, -1]
Explanation: Deposit 50 -> 50; withdraw 80 exceeds balance, rejected -> -1 (balance stays 50).
Input: ([[1, 10]],)
Expected Output: [-1]
Explanation: Withdraw 10 on an empty account is rejected -> -1.
Input: ([],)
Expected Output: []
Explanation: No operations -> empty result list.
Input: ([[0, 200], [2, 5], [1, 210], [1, 5]],)
Expected Output: [200, 210, 0, -1]
Explanation: Deposit 200 -> 200; cashback 5% = 10 -> 210; withdraw 210 (= balance) -> 0; withdraw 5 exceeds balance 0, rejected -> -1.
Input: ([[0, -5], [0, 100], [2, -1]],)
Expected Output: [-1, 100, -1]
Explanation: Deposit -5 invalid -> -1; deposit 100 -> 100; cashback with negative percent invalid -> -1.
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.