In-Memory Banking System — Design and Implementation
You are to design and implement an in-memory banking system that supports account management and transactions, and can report the top-k accounts by their cumulative outgoing transfers.
Operations
-
createAccount(accountId, initialBalance = 0)
-
Creates a new account with the given ID and initial balance (default 0).
-
deposit(accountId, amount)
-
Adds funds to an existing account.
-
transfer(fromId, toId, amount)
-
Debits the sender (fromId) and credits the receiver (toId).
-
Reject the transfer if the sender has insufficient funds.
-
Maintain, for each account, the cumulative outgoing transaction amount (sum of all successful debits initiated by that account).
Reporting
-
topNAccountsByOutgoing(n)
-
Returns the top n accounts by total outgoing amount, breaking ties by accountId ascending.
-
Output format: a list of strings formatted as "{outgoingAmount}:{accountId}".
Requirements
-
Describe the data structures you will use.
-
Specify error handling for cases such as unknown account, invalid amounts, and insufficient funds.
-
Provide pseudocode for both:
-
a full-sort solution, and
-
a heap-based top-k solution.
-
Analyze time and space complexity for each operation and for both top-k approaches.