Quick Overview

This question evaluates implementation skills in sequential operation processing, integer arithmetic, control flow, edge-case handling, and overflow awareness within algorithmic constraints.

Implement bank account with cashback

Company: Coinbase

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

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.

Quick Answer: 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. 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` — add `x` to the balance. - `WITHDRAW x` — subtract `x` from the balance only if the current balance is `>= x`; otherwise ignore the operation. - `CASHBACK p` — credit `floor(balance * p / 100)` to the balance (i.e. add `p` percent 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

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

  1. 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.
  2. Parse each operation by splitting on whitespace into a command and an integer value.
  3. For WITHDRAW, guard with `if balance >= x` so an oversized withdrawal is silently skipped rather than driving the balance negative.
  4. For CASHBACK, compute the credit as integer floor division: `(balance * p) // 100`. Keep everything in 64-bit integers so `balance * p` does not overflow.

Loading coding console...