Your question is In-Memory Banking Operations. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Implement a transaction processor for Capital One accounts. The processor must support deposits, withdrawals, transfers, and queries for the most active accounts.
An account is identified by a unique string and has a nonnegative integer balance. An account's activity score is the total value of all successful withdrawals and outgoing transfers from that account. Deposits and incoming transfers do not increase the score.
Implement account_operations(accounts, operations), where accounts is a dictionary mapping account IDs to initial balances, and operations is a list of operation arrays:
["deposit", account_id, amount]["withdraw", account_id, amount]["transfer", source_id, destination_id, amount]["top", n]Return one result for every operation that changes state or queries state. Deposit, withdrawal, and transfer return true on success and false otherwise. A transaction fails if an account is unknown, an amount is not positive, or the source balance is insufficient. A transfer must be atomic. A top query returns up to n account IDs sorted by descending activity score, then lexicographically by account ID.
def account_operations(accounts, operations):