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.
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.