Banking System
AirbnbOnline assessmentHard
You are building an in-memory banking system as the class BankingSystem. Every method takes the call's timestamp as its first argument.
Part 1: Accounts, deposits and transfers
Implement:
create_account(timestamp, account_id)creates an account with balance 0 and returnstrue, or returnsfalseif an account with that id already exists.deposit(timestamp, account_id, amount)addsamountto the account and returns its new balance, or returnsnullif the account does not exist.transfer(timestamp, source_account_id, target_account_id, amount)movesamountfrom the source to the target immediately and returns the source's new balance. It returnsnull, changing nothing, when either account does not exist, the two ids are equal, or the source holds less thanamount.
Ids are case sensitive.
- Example 1
- init
[]- operations
[["create_account",[1,"a"]],["create_account",[2,"a"]],["create_account",[3,"b"]],["deposit",[4,"a",100]],["deposit",[5,"z",5]],["transfer",[6,"a","b",30]],["transfer",[7,"a","a",1]],["transfer",[8,"a","z",1]],["transfer",[9,"z","a",1]],["transfer",[10,"b","a",31]],["transfer",[11,"b","a",30]],["deposit",[12,"b",1]],["transfer",[13,"a","b",100]]]- Output
[true,false,true,100,null,70,null,null,null,null,0,1,0]
Why:
aandbare created; the second creation ofafails. After 100 is deposited intoa, transferring 30 tobreturnsa's new balance, 70. A transfer to the same account, to or from an unknown account, or of more than the source holds returnsnull.bholds exactly 30, so moving 30 back succeeds and leaves 0, andacan then move its whole balance of 100.
Constraints
1 <= timestamp <= 10^9, andtimestampis larger in every call than in the call before1 <= len(account_id) <= 32, and an id uses ASCII letters and digits1 <= amount <= 10^12- At most
2000calls in total
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.