KatInterview

Simulate Account Transactions with Integer Cashbacks

Math & Bit ManipulationAuto-graded · 17 test cases
EasyAuto-gradedMath & Bit Manipulationsimulationmatharrays

Last updated

Process a series of deposits, conditional withdrawals, and integer floor cashbacks to compute a final account balance.

You are tasked with maintaining a ledger for an account balance. Starting with an initial integer balance, you must process a sequence of transactions in the exact order they are given and return the resulting balance.

Each transaction is represented as a string containing an operation type followed by a non-negative integer parameter space-separated:

  1. "DEPOSIT x": Increase the current balance by x.
  2. "WITHDRAW x": Decrease the current balance by x only if the current balance is at least x. If the balance is less than x, the transaction is skipped.
  3. "CASHBACK p": Increase the current balance by floor(balance * p / 100).

Constraints

  • 0 <= balance <= 10^18
  • 0 <= len(operations) <= 10^5
  • For each operation parameter x or p: 0 <= x, p <= 10^9.
  • All operations follow the exact format "<TYPE> <VALUE>".

Examples

Example 1

Input: process_bank_account(100, ['DEPOSIT 50', 'WITHDRAW 120', 'CASHBACK 10', 'WITHDRAW 15'])

Output: 18

Hints

Hint 1

Iterate through the array of transaction strings step-by-step to maintain a running balance.

Hint 2

Ensure withdrawals are only executed if the current balance is greater than or equal to the requested amount.

Hint 3

Use exact integer arithmetic for cashback calculations (`balance * p // 100`) rather than floating-point math to prevent precision errors.