Implement bank account with cashback
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates implementation skills in sequential operation processing, integer arithmetic, control flow, edge-case handling, and overflow awareness within algorithmic constraints.
Constraints
- 0 <= B (initial balance fits in a 64-bit integer)
- 0 <= n <= 1e5 (number of operations)
- Each operation is one of DEPOSIT x, WITHDRAW x, CASHBACK p
- x and p are non-negative integers
- WITHDRAW x is ignored if balance < x
- CASHBACK p credits floor(balance * p / 100)
- Use 64-bit arithmetic to avoid overflow
Examples
Input: (100, ["DEPOSIT 50", "WITHDRAW 120", "CASHBACK 10", "WITHDRAW 15"])
Expected Output: 18
Explanation: 100 +50=150; withdraw 120 (150>=120) ->30; cashback 10% = floor(3)=3 ->33; withdraw 15 (33>=15) ->18.
Input: (0, [])
Expected Output: 0
Explanation: Empty operation list leaves the initial balance of 0 unchanged.
Input: (50, ["WITHDRAW 100"])
Expected Output: 50
Explanation: Balance 50 < 100 so the withdrawal is ignored and the balance stays 50.
Input: (100, ["CASHBACK 0", "DEPOSIT 0", "WITHDRAW 0"])
Expected Output: 100
Explanation: Zero-amount operations are all no-ops: 0% cashback adds 0, depositing 0 adds 0, withdrawing 0 (100>=0) subtracts 0.
Input: (100, ["CASHBACK 50", "CASHBACK 50", "CASHBACK 50"])
Expected Output: 337
Explanation: 100 -> +50=150 -> +75=225 -> +floor(112.5)=112 ->337. Consecutive cashbacks compound off the updated balance with floor rounding.
Input: (1000000000000, ["DEPOSIT 1000000000000", "CASHBACK 100"])
Expected Output: 4000000000000
Explanation: 1e12 + 1e12 = 2e12; 100% cashback adds another 2e12 -> 4e12. Requires 64-bit integers.
Input: (99, ["CASHBACK 99"])
Expected Output: 197
Explanation: floor(99*99/100)=floor(98.01)=98; 99+98=197, exercising floor division on a non-round percentage.
Hints
- Process the operations in a single left-to-right pass; you only need one running balance variable, giving O(n) time and O(1) extra space.
- Parse each operation by splitting on whitespace into a command and an integer value.
- For WITHDRAW, guard with `if balance >= x` so an oversized withdrawal is silently skipped rather than driving the balance negative.
- For CASHBACK, compute the credit as integer floor division: `(balance * p) // 100`. Keep everything in 64-bit integers so `balance * p` does not overflow.