Process Account Transaction Sequence
Data Structure DesignAuto-graded · 20 test casesLast 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]- Addamountto the account's balance["withdraw", account_id, amount]- Subtractamountfrom the account's balance["transfer", from_id, to_id, amount]- Moveamountfrom one account to another
Validation Rules:
An operation fails (returns false) if any of these conditions hold:
- The account ID is out of range (must be between 1 and n inclusive, where n is the number of accounts)
- The amount is negative
- For withdrawals: the account has insufficient funds
- For transfers: the source account has insufficient funds
- 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
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The solution simulates each operation by maintaining a running state of account balances. For each operation, we follow a validate-then-execute pattern:
- Parse the operation type and extract the relevant parameters
- Validate all preconditions without modifying state:
- Account IDs must be valid integers within range [1, n]
- Amounts must be non-negative
- For withdrawals and transfers, source accounts must have sufficient funds
- Execute the operation only if all validations pass, updating the balance array
- Record the result (true for success, false for failure)
The key insight is that validation must happen atomically before any state changes. For a transfer, we check both the source account's funds and the validity of both account IDs before debiting or crediting either account. This ensures that failed operations leave the system in its previous state.
Why It Works
Each operation is independent and executes in sequence. By validating all conditions before making any modifications, we guarantee that:
- Failed operations have no side effects
- Successful operations update state correctly for subsequent operations
- The balance array always represents a valid state
The 1-indexed account IDs map directly to 0-indexed array positions by subtracting 1, making lookups straightforward.
Complexity
- Time: O(m) where m is the number of operations. Each operation performs constant-time validation and updates.
- Space: O(m) for the result array. The balance array is modified in-place and counts as input space.
With up to 200,000 operations, this solution runs efficiently with minimal overhead per operation.
Solution Code
from typing import List
def process_account_operations(balances: List[int], operations: List[List]) -> List[bool]:
n = len(balances)
results = []
for operation in operations:
op_type = operation[0]
if op_type == "deposit":
account_id, amount = operation[1], operation[2]
if not isinstance(account_id, int) or not 1 <= account_id <= n or amount < 0:
results.append(False)
else:
balances[account_id - 1] += amount
results.append(True)
elif op_type == "withdraw":
account_id, amount = operation[1], operation[2]
if (not isinstance(account_id, int) or not 1 <= account_id <= n
or amount < 0 or balances[account_id - 1] < amount):
results.append(False)
else:
balances[account_id - 1] -= amount
results.append(True)
elif op_type == "transfer":
from_id, to_id, amount = operation[1], operation[2], operation[3]
if (not isinstance(from_id, int) or not isinstance(to_id, int)
or not 1 <= from_id <= n or not 1 <= to_id <= n
or amount < 0 or balances[from_id - 1] < amount):
results.append(False)
else:
balances[from_id - 1] -= amount
balances[to_id - 1] += amount
results.append(True)
else:
results.append(False)
return results