← 返回 meta 的题目列表Design a Banking System (Progressive OOD, 4 Levels)
类型:online_judge
Progressive OOD: Banking System (4 Levels)
Implement an in-memory banking system that supports account creation, deposits, transfers, spender ranking, scheduled payments, payment cancellation, account merges, and historical balance queries.
Each API call includes an integer timestamp. Follow the specified return values and error conditions.
Note: Outgoing refers only to money sent from an account as the source of transfer; deposits do not count as outgoing.
Level 1 - Basic account operations
Implement:
create_account(timestamp, account_id) -> bool
Create the account if it does not exist and return True.
If it already exists, return False.
deposit(timestamp, account_id, amount) -> int | None
Deposit amount into the account.
Return the new balance.
If the account does not exist, return None.
transfer(timestamp, source, target, amount) -> int | None
Transfer amount from source to target.
On success return the remaining balance of source.
Return None if any is true:
source or target does not exist
source == target
insufficient balance in source
Level 2 - Ranking (Top spenders)
Implement:
top_spenders(timestamp, n) -> list[str]
Return the top n accounts by total outgoing amount.
Output format: "account_id(total_outgoing)".
Break ties by ascending lexicographic account_id.
Only count outgoing from transfer where the account is the source.
Level 3 - Scheduled payments
Implement:
schedule_payment(timestamp, account_id, amount, delay) -> str | None
Create a scheduled payment for account_id.
It should execute at timestamp + delay.
Return a globally increasing id: "payment1", "payment2", ...
If the account does not exist, return None.
cancel_payment(timestamp, account_id, payment_id) -> bool
Cancel the scheduled payment payment_id under account_id.
Return True if canceled, otherwise False.
Level 4 - Account merge + historical balance
Implement:
merge_accounts(timestamp, account_id_1, account_id_2) -> bool
Merge account_id_2 into account_id_1 and delete account_id_2.
Rules:
balances add up
total outgoing add up
move all scheduled payments from account_id_2 to account_id_1
Return whether the merge succeeds.
get_balance(timestamp, account_id, time_at) -> int | None
Return the account balance at time time_at.
Return None if the account does not exist or time_at is earlier than the account creation time.
I/O (typical CodeSignal style, for reference)
You may be given a list of queries executed in order; each query maps to one API call.
Output the return value for each query (bool/int/string/list/null).
Sample tests (5 examples, expressed as call sequences -> return sequences)
Case 1: Level1 basics
Input
create_account(1,"A")
deposit(2,"A",100)
create_account(3,"A")
transfer(4,"A","B",10)
create_account(5,"B")
transfer(6,"A","B",30)
Output
[true, 100, false, null, true, 70]
Case 2: transfer failures
Input
create_account(1,"A")
create_account(2,"B")
deposit(3,"A",20)
transfer(4,"A","A",5)
transfer(5,"A","B",50)
transfer(6,"C","B",1)
Output
[true, true, 20, null, null, null]
Case 3: top spenders + tie break
Input
create_account(1,"A")
create_account(1,"B")
create_account(1,"C")
deposit(2,"A",100)
deposit(2,"B",100)
deposit(2,"C",100)
transfer(3,"A","B",40)
transfer(4,"B","C",40)
transfer(5,"C","A",10)
top_spenders(6,2)
Output
[true,true,true,100,100,100,60,60,90,["A(40)","B(40)"]]
Case 4: schedule + cancel ids
Input
create_account(1,"A")
schedule_payment(2,"A",10,5)
schedule_payment(3,"A",20,5)
cancel_payment(4,"A","payment1")
cancel_payment(5,"A","payment1")
Output
[true,"payment1","payment2",true,false]
Case 5: merge + get_balance
Input
create_account(1,"A")
create_account(1,"B")
deposit(2,"A",50)
deposit(3,"B",70)
merge_accounts(4,"A","B")
get_balance(5,"A",3)
get_balance(6,"B",3)
Output
[true,true,50,70,true,120,null]
Suggested constraints (if needed)
Number of accounts and operations can be large; aim for around O(q log q) or better.
Handle edge cases: missing accounts, duplicates, invalid transfers, deletion after merge, etc.
Example
Input
create_account(1,"A")
deposit(2,"A",100)
create_account(3,"A")
transfer(4,"A","B",10)
create_account(5,"B")
transfer(6,"A","B",30)
Output
true
100
false
null
true
70