KatInterview

Process Account Transaction Sequence

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designsimulationvalidationstate-management

Last updated

Tests ability to implement stateful operations on indexed data with validation, edge-case handling, and multiple operation types.

You are given an array of account balances and a sequence of financial operations to execute. Each account is identified by a 1-indexed position in the balance array.

Your task is to process each operation in order and return whether each operation succeeded or failed.

Operations:

  • ["deposit", account_id, amount] - Add amount to the account's balance
  • ["withdraw", account_id, amount] - Subtract amount from the account's balance
  • ["transfer", from_id, to_id, amount] - Move amount from one account to another

Validation Rules:

An operation fails (returns false) if any of these conditions hold:

  1. The account ID is out of range (must be between 1 and n inclusive, where n is the number of accounts)
  2. The amount is negative
  3. For withdrawals: the account has insufficient funds
  4. For transfers: the source account has insufficient funds
  5. The operation type is unrecognized

When an operation fails, account balances remain unchanged. When an operation succeeds, balances are updated immediately and affect subsequent operations.

Constraints:

  • 1 ≤ number of accounts ≤ 100,000
  • 1 ≤ number of operations ≤ 200,000
  • 0 ≤ initial balance ≤ 10^12
  • 0 ≤ amount ≤ 10^12
  • Account IDs and amounts are integers
  • Operations are processed sequentially in a single-threaded environment

Examples

Example 1

Input: process_account_operations([100, 50, 25], [['deposit', 1, 25], ['withdraw', 2, 10], ['transfer', 1, 3, 40]])

Output: [True, True, True]

Example 2

Input: process_account_operations([0, 100], [['withdraw', 1, 1], ['transfer', 2, 1, 100]])

Output: [False, True]

Hints

Hint 1

Validate all conditions before modifying any balances to ensure failed operations have no side effects

Hint 2

Remember that account IDs are 1-indexed, so account 1 corresponds to index 0 in the balance array

Hint 3

For transfers, check that the source has sufficient funds before debiting; the destination account doesn't need a balance check