Airbnb questions

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 returns true, or returns false if an account with that id already exists.
  • deposit(timestamp, account_id, amount) adds amount to the account and returns its new balance, or returns null if the account does not exist.
  • transfer(timestamp, source_account_id, target_account_id, amount) moves amount from the source to the target immediately and returns the source's new balance. It returns null, changing nothing, when either account does not exist, the two ids are equal, or the source holds less than amount.

Ids are case sensitive.

  1. 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: a and b are created; the second creation of a fails. After 100 is deposited into a, transferring 30 to b returns a's new balance, 70. A transfer to the same account, to or from an unknown account, or of more than the source holds returns null. b holds exactly 30, so moving 30 back succeeds and leaves 0, and a can then move its whole balance of 100.

Constraints

  • 1 <= timestamp <= 10^9, and timestamp is larger in every call than in the call before
  • 1 <= len(account_id) <= 32, and an id uses ASCII letters and digits
  • 1 <= amount <= 10^12
  • At most 2000 calls in total
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question