Coding questions · Coinbase
Implement bank account with cashback
This question evaluates implementation skills in sequential operation processing, integer arithmetic, control flow, edge-case handling, and overflow awareness within algorithmic constraints.
Implement a bank account processor. Given an initial non-negative integer balance B and a list of operations (n up to 1e 5), apply each operation in order and return the final balance. Operations are: (1) DEPOSIT x — add x to the balance; (2) WITHDRAW x — subtract x only if balance ≥ x, otherwise ignore the operation; (3) CASHBACK p — credit floor(balance * p / 100) to the balance. Assume all x and p are non-negative integers; use 64-bit integers to avoid overflow. Target O(n) time and O(1) extra space. Provide unit tests for edge cases (zero amounts, multiple consecutive cashbacks, ignored withdrawals, large values). Example: B=100, ops=["DEPOSIT 50","WITHDRAW 120","CASHBACK 10","WITHDRAW 15"] → 18.
Implement Bank Account with Cashback
Implement a bank account processor. You are given an initial non-negative integer balance B and a list of ops (n up to 1e5). Apply each operation in order and return the final balance.
Each operation is a string of the form "COMMAND value":
DEPOSIT x— addxto the balance.WITHDRAW x— subtractxfrom the balance only if the current balance is>= x; otherwise ignore the operation.CASHBACK p— creditfloor(balance * p / 100)to the balance (i.e. addppercent of the current balance, rounded down).
All x and p are non-negative integers. Use 64-bit integers to avoid overflow. Aim for O(n) time and O(1) extra space.
Example: B = 100, ops = ["DEPOSIT 50", "WITHDRAW 120", "CASHBACK 10", "WITHDRAW 15"] → 18.
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
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 >= xso 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 sobalance * pdoes not overflow.