Problem · Design

Banking System, Part 2: Top Spenders

Learn this problem
MediumAnthropicOA

Problem statement

Banking System series

Continue the banking system from Part 1. All Level 1 operations keep the same behavior, and the system now ranks accounts by total outgoing transactions.

Operation Format

Each row in operations contains an operation name followed by string arguments. Return one row per operation. Scalar and null results use a one-element row. TOP_SPENDERS returns its list directly as the result row.

Previous Operations

CREATE_ACCOUNT, DEPOSIT, and TRANSFER behave exactly as described in Part 1. Only successful transfers add their amount to the source account's outgoing total.

Level 2 Operation

  • ["TOP_SPENDERS", timestamp, n]: Return up to n active accounts with the highest total outgoing amount.
  • Sort by outgoing amount in descending order. Break ties by account_id in ascending alphabetical order.
  • Format each result as account_id(total_outgoing).
  • If fewer than n accounts exist, return all existing accounts.

At this level, outgoing transactions come from successful transfers. Part 3 also counts successfully executed scheduled payments.

Function

bankingSystemLevel2(operations: String[][]) → String[][]

Examples

Example 1

operations = [["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["CREATE_ACCOUNT","3","cara"],["DEPOSIT","4","alice","500"],["DEPOSIT","5","bob","500"],["DEPOSIT","6","cara","500"],["TRANSFER","7","alice","bob","100"],["TRANSFER","8","bob","cara","100"],["TOP_SPENDERS","9","2"],["TRANSFER","10","bob","alice","50"],["TOP_SPENDERS","11","3"]]return = [["true"],["true"],["true"],["500"],["500"],["500"],["400"],["500"],["alice(100)","bob(100)"],["450"],["bob(150)","alice(100)","cara(0)"]]

Alice and Bob first tie at 100 outgoing, so Alice comes first alphabetically. Bob's next transfer raises his total to 150.

Example 2

operations = [["CREATE_ACCOUNT","1","zed"],["CREATE_ACCOUNT","2","amy"],["DEPOSIT","3","zed","20"],["TRANSFER","4","zed","amy","30"],["TOP_SPENDERS","5","5"],["TRANSFER","6","zed","amy","10"],["TOP_SPENDERS","7","5"]]return = [["true"],["true"],["20"],["null"],["amy(0)","zed(0)"],["10"],["zed(10)","amy(0)"]]

The failed transfer does not count as outgoing. With both totals at zero, Amy comes first alphabetically. The later successful transfer gives Zed an outgoing total of 10.

Constraints

  • 1 <= timestamp <= 10^9
  • All timestamps are unique and operations are provided in strictly increasing timestamp order.
  • n and all amounts are positive integers.
  • All numeric results fit in a signed 64-bit integer.
  • Every operation has exactly the arguments defined in Parts 1 and 2.

More Anthropic problems

drafts saved locally
public String[][] bankingSystemLevel2(String[][] operations) {
  // write your code here
}
operations[["CREATE_ACCOUNT","1","alice"],["CREATE_ACCOUNT","2","bob"],["CREATE_ACCOUNT","3","cara"],["DEPOSIT","4","alice","500"],["DEPOSIT","5","bob","500"],["DEPOSIT","6","cara","500"],["TRANSFER","7","alice","bob","100"],["TRANSFER","8","bob","cara","100"],["TOP_SPENDERS","9","2"],["TRANSFER","10","bob","alice","50"],["TOP_SPENDERS","11","3"]]
expected[["true", "true", "true", "500", "500", "500", "400", "500", "alice(100)", "bob(100)", "450", "bob(150)", "alice(100)", "cara(0)"]]
checking account