Design a Banking Component with Transfers and Activity Rankings
Capital OneSoftware Engineer · OnsiteLast updated
Design an in-memory banking service supporting account creation, deposits, atomic transfers, and activity rankings driven by a string-based command interface.
Problem Statement
Design an in-memory banking engine that handles account lifecycle management, deposits, atomic peer-to-peer transfers, and activity rankings. You will implement a class BankSystem along with a command parser process_transactions(commands: list[str]) -> list[str] to execute queries sequentially.
Commands to Support
-
CREATE- Creates a new account with a balance of
0and cumulative outgoing transfer volume of0. - Returns
"true"if successful, or"false"ifaccount_idalready exists.
- Creates a new account with a balance of
-
DEPOSIT- Deposits positive integer
amountintoaccount_id. - Returns the updated balance as a string if successful.
- Returns
""(empty string) if the account does not exist oramount - Transfers
amountfromsource_idtotarget_id. - Must satisfy invariants:
source_idandtarget_idboth exist,source_id != target_id,amount > 0, andsource_idhas a balance >=amount. - If valid, deduct
amountfromsource_id, addamounttotarget_id, and incrementsource_id's total outgoing transfer volume byamount. - Returns
"true"if successful, or"false"if any validation fails (no state changes on failure).
- Deposits positive integer
-
TOP_ACTIVITY- Returns the top
kaccount IDs ranked by cumulative outgoing transfer volume in descending order. - In case of a tie in volume, sort lexicographically (alphabetical order) by
account_idascending. - If fewer than
kaccounts exist, return all accounts in ranked order. - Format output as a comma-separated list of IDs, e.g.
"ACC1,ACC2". If no accounts exist or `k list[str]: """ Executes a sequence of string commands and returns the result of each command. """ pass
- Returns the top
---
## Example
### Input
```python
commands = [
"CREATE A",
"CREATE B",
"CREATE C",
"DEPOSIT A 100",
"DEPOSIT B 50",
"TRANSFER A B 30",
"TRANSFER B C 20",
"TRANSFER A C 10",
"TOP_ACTIVITY 2",
"TRANSFER C A 100", # C only has 30, should fail
"TOP_ACTIVITY 3"
]
Output
[
"true",
"true",
"true",
"100",
"50",
"true",
"true",
"true",
"A,B",
"false",
"A,B,C"
]
Explanation
- After transfers:
Ahas transferred30 + 10 = 40,Bhas transferred20, andChas transferred0. TOP_ACTIVITY 2returns"A,B".TRANSFER C A 100fails becauseChas a balance of30(fromTRANSFER B C 20andTRANSFER A C 10), so balance is insufficient.TOP_ACTIVITY 3returns"A,B,C".
Constraints
1 <= len(commands) <= 20,000- Account IDs consist of alphanumeric characters (
[a-zA-Z0-9]), length1 <= len(account_id) <= 32. 1 <= amount <= 1,000,000,0001 <= k <= 1,000- Balances and transfer amounts fit within standard 64-bit signed integers.
Edge Cases
- Attempting to transfer to oneself (
source_id == target_id). - Insufficient funds or zero/negative transaction amounts.
- Operations on non-existent accounts.
klarger than the total number of created accounts.
Hints
Hint 1
Maintain a dedicated Account class or dictionary mapping account IDs to an object tracking balance and total outgoing transfer volume.
Hint 2
Validate all preconditions before modifying any balances in the transfer operation to ensure atomicity.
Hint 3
For activity rankings, sort the accounts using a compound key `(-outgoing_volume, account_id)` to handle the descending volume and ascending alphabetical order simultaneously.
Follow-up questions
- How would you optimize `TOP_ACTIVITY` if calls are frequent and the total number of accounts is very large (e.g., millions)?
- How would you adapt the system to handle concurrent requests safely across multiple threads?
- How would you extend this design to support transaction rollbacks or scheduled pending transfers with an expiration window?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.