KatInterview

Execute Banking Operations with Transaction History

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

Last updated

Tests command-pattern operation execution, input validation, atomic state transitions, and maintaining per-entity audit logs.

You are given a sequence of banking operations to execute against an in-memory account ledger. Each operation is specified by a command name and a list of arguments. Process all operations in order and return the result of each.

Operations

open_account(account_id)

  • Creates a new account with the given identifier and zero balance
  • Returns True if successful
  • Returns False if an account with that identifier already exists

deposit(account_id, amount)

  • Adds amount to the specified account's balance
  • Returns True if successful
  • Returns False if the account does not exist or the amount is invalid

withdraw(account_id, amount)

  • Subtracts amount from the specified account's balance
  • Returns True if successful
  • Returns False if the account does not exist, the amount is invalid, or insufficient funds are available

transfer(source_id, destination_id, amount)

  • Moves amount from the source account to the destination account atomically
  • Returns True if successful
  • Returns False if either account does not exist, the accounts are identical, the amount is invalid, or the source has insufficient funds

balance(account_id)

  • Returns the current balance as an integer
  • Returns None if the account does not exist

history(account_id)

  • Returns a list of transaction records in chronological order
  • Each deposit creates a record: {"type": "deposit", "amount": <value>}
  • Each withdrawal creates a record: {"type": "withdraw", "amount": <value>}
  • Each transfer creates two records:
    • On the source: {"type": "transfer", "amount": <value>, "direction": "out", "counterparty": <dest_id>}
    • On the destination: {"type": "transfer", "amount": <value>, "direction": "in", "counterparty": <source_id>}
  • Returns None if the account does not exist
  • Failed operations do not create history records

Amount Validation

An amount is valid if and only if it is a positive integer (type int, not bool, greater than zero).

Constraints

  • 1 ≤ number of operations ≤ 1000
  • Account identifiers are non-empty strings
  • All amounts fit in a 32-bit signed integer
  • Operations are processed sequentially; each sees the state resulting from all prior operations
  • Failed operations leave account balances and histories unchanged

Examples

Example 1

Input: execute_banking_operations(['open_account', 'deposit', 'withdraw', 'balance'], [['alice'], ['alice', 1000], ['alice', 250], ['alice']])

Output: [True, True, True, 750]

Example 2

Input: execute_banking_operations(['open_account', 'open_account', 'deposit', 'withdraw', 'withdraw', 'balance'], [['a'], ['a'], ['a', 50], ['a', 20], ['a', 40], ['a']])

Output: [True, False, True, True, False, 30]

Example 3

Input: execute_banking_operations(['open_account', 'open_account', 'deposit', 'transfer', 'balance', 'balance', 'history', 'history'], [['alice'], ['bob'], ['alice', 100], ['alice', 'bob', 35], ['alice'], ['bob'], ['alice'], ['bob']])

Output: [True, True, True, True, 65, 35, [{'type': 'deposit', 'amount': 100}, {'type': 'transfer', 'amount': 35, 'direction': 'out', 'counterparty': 'bob'}], [{'type': 'transfer', 'amount': 35, 'direction': 'in', 'counterparty': 'alice'}]]

Hints

Hint 1

Track both balances and transaction logs in separate data structures, initializing both when an account opens.

Hint 2

For transfers, verify all preconditions before modifying any state, then update both accounts and both histories as a single atomic step.

Hint 3

Amount validation must reject booleans explicitly since in Python, bool is a subclass of int.