FastPrepBanking System with Accepted Transfers, Part 2: Activity Ranking

Banking System with Accepted Transfers, Part 2: Activity Ranking

Ramp logoRamp● MediumNEW GRADOA
Learn

Problem statement

Implement a simplified banking system. The assessment is cumulative: after unlocking a new level, every operation from the current and previous levels remains available.

Query and Output Rules

  • Each query calls exactly one operation and includes a stringified millisecond timestamp.
  • Timestamps are unique, lie between 1 and 10^9, and appear in strictly increasing order.
  • Return one string result for every query, in input order.
  • Return the empty string when an operation has no successful scalar result, exactly as specified below.

Multipart Series

Level 1: Accounts and Payments

The banking system should support creating new accounts, depositing money, and withdrawing or paying money from accounts.

  • CREATE_ACCOUNT <timestamp> <accountId> creates a new account with the given accountId if it does not already exist. Return "true" if the account is created and "false" if it already exists.
  • DEPOSIT <timestamp> <accountId> <amount> deposits amount into the account. Return the account balance after processing the query, or the empty string if the account does not exist.
  • PAY <timestamp> <accountId> <amount> withdraws amount from the account. Return the account balance after processing the query. Return the empty string if the account does not exist or has insufficient funds.

Level 2: Activity Ranking

The banking system should support ranking accounts by the total value of their transactions.

  • TOP_ACTIVITY <timestamp> <n> returns the top n accounts with the highest total transaction value, sorted by total value descending and then by accountId alphabetically ascending.
  • Return one string in the format "<accountId1>(<transactionsValue1>), ..., <accountIdN>(<transactionsValueN>)".
  • Total transaction value is the sum of every processed amount for an account, regardless of how it changes the balance: deposits, payments, and each side of a successfully accepted transfer count.
  • If fewer than n accounts exist, return all active accounts in the same format.

Function

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

Examples

Example 1

queries = [["CREATE_ACCOUNT","1","account1"],["CREATE_ACCOUNT","2","account1"],["CREATE_ACCOUNT","3","account2"],["DEPOSIT","4","non-existing","2700"],["DEPOSIT","5","account1","2700"],["PAY","6","non-existing","2700"],["PAY","7","account1","2701"],["PAY","8","account1","200"]]return = ["true","false","true","","2700","","","2500"]

The duplicate account creation fails. Missing-account operations and an overdraw return empty strings. The final payment succeeds and leaves account1 with 2500.

Example 2

queries = [["CREATE_ACCOUNT","1","account1"],["CREATE_ACCOUNT","2","account2"],["CREATE_ACCOUNT","3","account3"],["DEPOSIT","4","account1","2000"],["DEPOSIT","5","account2","3000"],["DEPOSIT","6","account3","4000"],["TOP_ACTIVITY","7","3"],["PAY","8","account1","1500"],["PAY","9","account2","250"],["DEPOSIT","10","account3","250"],["TOP_ACTIVITY","11","3"]]return = ["true","true","true","2000","3000","4000","account3(4000), account2(3000), account1(2000)","500","2750","4250","account3(4250), account1(3500), account2(3250)"]

The first ranking follows the three deposits. Payments count toward total activity even though they reduce balances, so the second ranking uses totals 4250, 3500, and 3250.

Constraints

  • 1 <= queries.length <= 500.
  • Every timestamp is a unique integer from 1 through 10^9, and queries are supplied in strictly increasing timestamp order.
  • Every query row is well formed and uses an operation available at this part.
  • Amounts and n are positive base-10 integers. Every balance and transaction total fits in a signed 64-bit integer.
  • Account identifiers are non-empty strings that do not contain commas or parentheses.

More Ramp problems

See Ramp hiring insights
public String[] bankingAcceptedTransfersLevel2(String[][] queries) {
  // Process the queries in order and return one string per query.
  // write your code here
}
queries[["CREATE_ACCOUNT","1","account1"],["CREATE_ACCOUNT","2","account1"],["CREATE_ACCOUNT","3","account2"],["DEPOSIT","4","non-existing","2700"],["DEPOSIT","5","account1","2700"],["PAY","6","non-existing","2700"],["PAY","7","account1","2701"],["PAY","8","account1","200"]]
expected["true", "false", "true", "", "2700", "", "", "2500"]
Checking account…