KatInterview

Ledger Simulator with Holds, Deferred Payments and Cancellations

Data Structure DesignAuto-graded · 22 test cases
EasyAuto-gradedData Structure Designsimulationhash-mapstate-management

Last updated

Simulate a small in-memory bank driven by a command script, covering account creation, instant and deferred transfers, batch settlement and cancellation of pending transfers.

Ledger Simulator

You are given a script for a tiny in-memory bank. The script is two parallel lists: commands[i] is the name of the i-th command, and params[i] is the list of arguments for that command. Run the commands in order and return a list with one result per command, in the same order.

State

  • Every account is identified by a string user ID and holds an integer balance. A new account starts with a balance of 0. There is no deposit command.
  • Deferred transfers are tracked in a record. Each record stores the sender, the recipient, the amount and a status that is one of pending, completed or deleted.
  • Deferred transfer IDs are strings of the form "transfer-1", "transfer-2", and so on. The counter starts at 1 and increases by one only each time a deferred transfer is successfully created.

Commands

create_user, with params = [user_id]

  • If the ID is already in use, return False and change nothing.
  • Otherwise create the account with balance 0 and return True.

get_balance, with params = [user_id]

  • Return the user's current balance, or None if the user does not exist.
  • Funds held by a pending deferred transfer are not part of the sender's balance (see offline_transfer).

transfer, with params = [from_id, to_id, amount]

  • The transfer is invalid if any of these holds:
    • either user does not exist
    • from_id == to_id
    • amount <= 0
    • the sender's balance is less than amount
  • If it is invalid, return False and change nothing.
  • Otherwise move amount from the sender to the recipient right away and return True.

offline_transfer, with params = [from_id, to_id, amount]

  • It is invalid under exactly the same rules as transfer. If invalid, return False and change nothing.
  • Otherwise subtract amount from the sender's balance immediately (the money is held), do not credit the recipient yet, create a pending record, and return the new transfer ID string.

process_offline_transfers, with params = []

  • Settle every pending record, in the order the records were created: add its amount to the recipient's balance and mark it completed.
  • Return the number of records settled in this call (an integer, possibly 0).

delete_transfer, with params = [transfer_id]

  • If no record has that ID, or the record is not pending (already completed or deleted), return False and change nothing.
  • Otherwise give the held amount back to the sender, mark the record deleted, and return True. A deleted record can never be settled later.

Any other command name returns False and changes nothing.

Task

Implement run_bank_script(commands, params) that executes the script and returns the list of results.

Constraints

  • len(commands) == len(params).
  • User IDs and transfer IDs are strings; amounts are integers.
  • Result types are exactly as above: booleans, integers, strings, or None.
  • Money is conserved: total balances plus pending holds never change except through the commands above.
  • Performance is not the focus; a straightforward simulation is enough.

Examples

Example 1

Input: run_bank_script(['create_user', 'create_user', 'transfer', 'get_balance', 'offline_transfer', 'get_balance', 'process_offline_transfers', 'get_balance', 'delete_transfer'], [['alice'], ['bob'], ['alice', 'bob', 30], ['alice'], ['alice', 'bob', 20], ['alice'], [], ['bob'], ['transfer-1']])

Output: [True, True, False, 0, False, 0, 0, 0, False]

Example 2

Input: run_bank_script(['create_user', 'create_user', 'offline_transfer', 'delete_transfer', 'get_balance', 'process_offline_transfers'], [['a'], ['b'], ['a', 'b', 10], ['transfer-1'], ['a'], []])

Output: [True, True, False, False, 0, 0]

Hints

Hint 1

Keep two dictionaries: one mapping user ID to balance, and one mapping transfer ID to a record holding sender, recipient, amount and status. Python dicts preserve insertion order, which gives you creation order for settlement.

Hint 2

Write one validity check and use it for both transfer and offline_transfer. Validate everything first and only mutate state after all checks pass, so a failure never leaves a partial change.

Hint 3

For offline_transfer, take the money from the sender at creation time. Settling then only credits the recipient, and deleting only refunds the sender, so only pending records may be settled or deleted. Increase the ID counter only on success.