KatInterview

Replay Ledger Commands on a Guarded Account

Data Structure DesignAuto-graded · 22 test cases
EasyAuto-gradedData Structure Designsimulationclass-designstate-management

Last updated

Simulate an account that processes a list of deposit, withdraw, balance and history commands, rejecting invalid amounts and overdrafts while recording only successful transactions.

You are given the starting balance of an account and a sequence of commands to run against it, in order. Write a function that processes the commands and returns one result per command.

Commands

Each command has a name and a list of arguments. The i-th entry of operations is paired with the i-th entry of arguments.

CommandArgumentsBehaviorResult
"deposit"[amount]If amount > 0, add it to the balance and record a ledger entry {"type": "deposit", "amount": amount}. Otherwise do nothing.True if applied, False if rejected
"withdraw"[amount]If amount > 0 and amount is not greater than the current balance, subtract it and record a ledger entry {"type": "withdrawal", "amount": amount}. Otherwise do nothing.True if applied, False if rejected
"get_balance"[]Reads the current balance.The balance
"get_transaction_history"[]Reads the ledger.A list of all ledger entries in the order they were recorded

Rules

  • The starting balance is not a ledger entry. The ledger only contains successful deposits and withdrawals.
  • A rejected command changes nothing: the balance stays the same and no ledger entry is added.
  • Withdrawing exactly the current balance is allowed and leaves a balance of 0.
  • Note the spelling: the command is "withdraw", but the ledger entry type is "withdrawal".
  • The history returned by a command must be a snapshot. Later commands must not alter a snapshot that was already returned.
  • If a command name is not one of the four above, raise ValueError.

Task

Implement the function so that it returns the list of results, one per command, in the same order as the commands.

Constraints

  • operations and arguments have the same length.
  • Amounts are numbers; zero and negative amounts can appear and must be rejected.
  • initial_balance is non-negative.
  • Commands are executed strictly sequentially in the given order.

Examples

Example 1

Input: replay_account_commands(100, ['deposit', 'withdraw', 'withdraw', 'get_balance', 'get_transaction_history'], [[50], [20], [200], [], []])

Output: [True, True, False, 130, [{'type': 'deposit', 'amount': 50}, {'type': 'withdrawal', 'amount': 20}]]

Example 2

Input: replay_account_commands(0, ['withdraw', 'deposit', 'get_balance', 'get_transaction_history'], [[1], [25], [], []])

Output: [False, True, 25, [{'type': 'deposit', 'amount': 25}]]

Example 3

Input: replay_account_commands(500, ['deposit', 'deposit', 'withdraw', 'get_transaction_history', 'get_balance'], [[100], [50], [200], [], []])

Output: [True, True, True, [{'type': 'deposit', 'amount': 100}, {'type': 'deposit', 'amount': 50}, {'type': 'withdrawal', 'amount': 200}], 450]

Hints

Hint 1

Keep three pieces of state while looping: the balance, the ledger list, and the list of results.

Hint 2

Rejection checks come first: a non-positive amount is rejected, and for withdrawals so is an amount greater than the balance. Rejected commands must not touch the balance or the ledger.

Hint 3

When returning the history, build a new list of copied entries so that later commands cannot change a snapshot you already returned.