Account Ledger
AirbnbMedium
You are building the transaction history of one bank account as the class AccountLedger. The balance starts at 0, and every call takes its time as a timestamp.
Part 1: Timestamped account
Implement:
deposit(timestamp, amount)records a deposit ofamountattimestamp. It returnsnull.withdraw(timestamp, amount)records a withdrawal ofamountattimestampand returnstrue, unlessamountis more than the account's current balance (the total of everything recorded so far). Then it returnsfalseand records nothing. A withdrawal of exactly the balance is allowed.transaction_history(start, end)returns[timestamp, change]for every recorded entry withstart <= timestamp <= end, in the order the entries were recorded.changeis positive for a deposit and negative for a withdrawal.balance(timestamp)returns the total of all recorded entries whose timestamp is at mosttimestamp.
Queries may ask about any time, earlier or later than the entries so far.
- Example 1
- init
[]- operations
[["deposit",[1,100]],["deposit",[5,50]],["withdraw",[5,30]],["withdraw",[7,200]],["withdraw",[8,120]],["balance",[0]],["balance",[1]],["balance",[5]],["balance",[7]],["balance",[8]],["transaction_history",[1,5]],["transaction_history",[2,4]],["transaction_history",[6,8]],["transaction_history",[0,100]]]- Output
[null,null,true,false,true,0,100,120,120,0,[[1,100],[5,50],[5,-30]],[],[[8,-120]],[[1,100],[5,50],[5,-30],[8,-120]]]
Why: Deposits of 100 (time 1) and 50 (time 5) and a withdrawal of 30 (time 5) leave 120. The withdrawal of 200 at time 7 is refused, since the balance is 120, and nothing is recorded. The withdrawal of 120 at time 8 empties the account and succeeds.
balance(5)counts all three entries at times 1 and 5.transaction_history(1, 5)includes the entries at both ends, in the order they were made.
Constraints
0 <= timestamp <= 10^9, and indepositandwithdrawcalls it is never smaller than in an earlierdepositorwithdrawcall0 <= start <= end <= 10^91 <= amount <= 10^9- 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.