Problem · Design

Banking System, Part 3: Scheduled Payments

Learn this problem
HardAnthropicOA

Problem statement

Banking System series

Continue the cumulative banking system from Parts 1 and 2. The system now supports scheduling and canceling payments.

Operation Format

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

Scheduled Payment Processing

  • Before processing an input operation at timestamp t, execute every pending payment whose scheduled time is at most t.
  • A payment scheduled for the same timestamp as another operation executes first.
  • Payments with the same execution timestamp are processed in creation order.
  • If the account has insufficient funds at execution time, the payment is skipped.
  • A successful payment reduces the balance and adds its amount to the account's outgoing total for TOP_SPENDERS.

Level 3 Operations

  • ["SCHEDULE_PAYMENT", timestamp, account_id, amount, delay]: Schedule a payment for timestamp + delay. Successful schedules receive globally unique IDs payment1, payment2, and so on. Return the ID, or null if the account does not exist.
  • ["CANCEL_PAYMENT", timestamp, account_id, payment_id]: Cancel a pending payment. Return true on success. Return false if the payment does not exist, is no longer pending, or belongs to a different account.

Because due payments execute first, canceling a payment at its execution timestamp is too late.

Function

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

Examples

Example 1

operations = [["CREATE_ACCOUNT","1","account1"],["CREATE_ACCOUNT","2","account2"],["DEPOSIT","3","account1","2000"],["DEPOSIT","4","account2","3000"],["SCHEDULE_PAYMENT","5","account1","50","10"],["SCHEDULE_PAYMENT","6","account2","1000","5"],["SCHEDULE_PAYMENT","7","account1","3000","7"],["DEPOSIT","11","account2","5"],["CANCEL_PAYMENT","12","account2","payment1"],["CANCEL_PAYMENT","13","account1","payment1"],["DEPOSIT","14","account1","5"],["DEPOSIT","15","account1","5"]]return = [["true"],["true"],["2000"],["3000"],["payment1"],["payment2"],["payment3"],["2005"],["false"],["true"],["2005"],["2010"]]

payment2 executes before the deposit at timestamp 11. payment1 is canceled by its owner. payment3 is skipped at timestamp 14 because Account 1 cannot cover 3000.

Example 2

operations = [["CREATE_ACCOUNT","1","a"],["DEPOSIT","2","a","100"],["SCHEDULE_PAYMENT","3","a","80","7"],["SCHEDULE_PAYMENT","4","a","30","6"],["CANCEL_PAYMENT","10","a","payment2"],["DEPOSIT","11","a","1"],["TOP_SPENDERS","12","1"]]return = [["true"],["100"],["payment1"],["payment2"],["false"],["21"],["a(80)"]]

Both payments are due at timestamp 10. payment1 executes first and leaves 20; payment2 is then skipped. The cancellation is processed afterward and returns false.

Constraints

  • 1 <= timestamp <= 10^9
  • All input-operation timestamps are unique and strictly increasing.
  • Amounts, delays, and ranking limits are positive integers.
  • All numeric results and scheduled execution timestamps fit in a signed 64-bit integer.
  • Every operation has exactly the arguments defined in Parts 1 through 3.

More Anthropic problems

drafts saved locally
public String[][] bankingSystemLevel3(String[][] operations) {
  // write your code here
}
operations[["CREATE_ACCOUNT","1","account1"],["CREATE_ACCOUNT","2","account2"],["DEPOSIT","3","account1","2000"],["DEPOSIT","4","account2","3000"],["SCHEDULE_PAYMENT","5","account1","50","10"],["SCHEDULE_PAYMENT","6","account2","1000","5"],["SCHEDULE_PAYMENT","7","account1","3000","7"],["DEPOSIT","11","account2","5"],["CANCEL_PAYMENT","12","account2","payment1"],["CANCEL_PAYMENT","13","account1","payment1"],["DEPOSIT","14","account1","5"],["DEPOSIT","15","account1","5"]]
expected[["true", "true", "2000", "3000", "payment1", "payment2", "payment3", "2005", "false", "true", "2005", "2010"]]
checking account