Problem · Design

Progressive Banking System

Learn this problem
HardAirbnb logoAirbnbFULLTIMEOA

Problem statement

Process queries in order. Each query contains an operation name followed by a strictly increasing non-negative timestamp and its arguments. Before processing an external query at timestamp t, execute every pending scheduled payment whose due time is at most t, ordered by due time and then payment creation order.

Return one string per external query, in order. Supported operations are:

  • ["CREATE_ACCOUNT", t, account]: create an account with balance and outgoing total zero. Return true, or false if the active account already exists.
  • ["DEPOSIT", t, account, amount]: add a positive amount. Return the new balance, or the empty string when the account is absent.
  • ["TRANSFER", t, source, target, amount]: move a positive amount between two distinct active accounts. Return the source balance after success, or the empty string when either account is absent, the accounts are equal, or funds are insufficient. A successful transfer adds the amount to the source account's outgoing total.
  • ["TOP_SPENDERS", t, n]: among active accounts, return at most n entries formatted account(outgoing), ordered by outgoing total descending and account identifier ascending, joined by commas.
  • ["SCHEDULE_PAYMENT", t, account, amount, delay]: schedule a positive payment for time t + delay, where delay is positive. If the account exists, return the next global identifier paymentK; otherwise return the empty string and do not consume an identifier.
  • ["CANCEL_PAYMENT", t, account, paymentId]: cancel a pending payment only when it currently belongs to that active account. Return true on success and false otherwise. A payment due at t is processed before this cancellation query.
  • ["MERGE_ACCOUNTS", t, survivor, absorbed]: merge two distinct active accounts. The survivor receives the absorbed balance and outgoing total; pending payments of the absorbed account now belong to the survivor; and the absorbed account closes at t. Return true on success and false otherwise.
  • ["GET_BALANCE", t, account, timeAt]: return that account's balance immediately after all activity at timestamp timeAt, or after the latest earlier activity when none occurred exactly then. Return the empty string if the account did not exist at that time. The requested time satisfies 0 <= timeAt <= t.

When a scheduled payment becomes due, deduct it only if its current account is active and has enough funds. A successful payment adds to outgoing total. Whether it succeeds or not, it is no longer pending. Balances and totals fit in signed 64-bit integers.

Function

processBankingQueries(queries: String[][]) → String[]

Examples

Example 1

queries = [["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["DEPOSIT","3","alice","100"],["TRANSFER","4","alice","bob","30"],["TOP_SPENDERS","5","2"]]return = ["true","true","100","70","alice(30),bob(0)"]

The transfer leaves alice with 70 and records 30 of outgoing activity, so she ranks before bob.

Example 2

queries = [["CREATE_ACCOUNT","1","a"],["DEPOSIT","2","a","50"],["SCHEDULE_PAYMENT","3","a","20","5"],["GET_BALANCE","7","a","7"],["GET_BALANCE","8","a","8"],["CANCEL_PAYMENT","9","a","payment1"]]return = ["true","50","payment1","50","30","false"]

The payment is still pending at timestamp 7. It executes before the query at timestamp 8, so cancellation afterward fails.

Example 3

queries = [["CREATE_ACCOUNT","1","a"],["CREATE_ACCOUNT","2","b"],["DEPOSIT","3","a","10"],["DEPOSIT","4","b","25"],["SCHEDULE_PAYMENT","5","b","5","5"],["MERGE_ACCOUNTS","6","a","b"],["GET_BALANCE","7","b","5"],["GET_BALANCE","8","b","6"],["GET_BALANCE","10","a","10"]]return = ["true","true","10","25","payment1","true","25","","30"]

Account b exists with balance 25 before the merge but is closed at timestamp 6. Its pending payment follows the survivor and reduces the combined balance from 35 to 30 at timestamp 10.

Constraints

  • 1 <= queries.length <= 2 * 10^4.
  • External query timestamps are strictly increasing integers from 0 through 10^9.
  • Every query has exactly one valid operation shape described above.
  • Account identifiers are non-empty printable ASCII strings without commas or parentheses.
  • Amounts and delays are positive integers, and every arithmetic result fits in a signed 64-bit integer.
  • 1 <= n <= 10^5 for TOP_SPENDERS.

More Airbnb problems

drafts saved locally
public String[] processBankingQueries(String[][] queries) {
    // Write your code here.
}
queries[["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["DEPOSIT","3","alice","100"],["TRANSFER","4","alice","bob","30"],["TOP_SPENDERS","5","2"]]
expected["true", "true", "100", "70", "alice(30)", "bob(0)"]
checking account