Problem · Simulation

Process List of Commands

MediumStripeNEW GRADOA
See Stripe hiring insights

The system simulates a transaction intent management tool to handle the process from initialization to completion and calculate the final balances of merchants' accounts. Transaction states include:

  • PENDING: The transaction is created and awaiting processing.
  • IN_PROGRESS: The transaction is being processed.
  • DONE: The transaction is completed, and the balance is updated.
  • Core Features:

  • START <account_id> <initial_balance> <refund_limit> (optional): Initializes a merchant account with an initial balance and an optional refund limit.
  • NEW <transaction_id> <account_id> <amount>: Creates a transaction intent for the specified account with the given amount in the PENDING state.
  • PROCESS <transaction_id>: Moves a transaction from PENDING to IN_PROGRESS.
  • COMPLETE <transaction_id>: Moves a transaction from IN_PROGRESS to DONE and adds the amount to the merchant's balance.
  • Extended Features:

  • MODIFY <transaction_id> <new_amount>: Updates the amount for a PENDING transaction intent.
  • CANCEL <transaction_id>: Reverts a transaction from IN_PROGRESS to PENDING.
  • RETURN <transaction_id>: Processes a refund for a DONE transaction and deducts the amount from the account's balance.
  • Time-Limited Refunds:

  • Each command includes a <time> marker indicating the timestamp.
  • Merchants can set a refund time limit; transactions can only be refunded within this limit.
    • If no limit is set, refunds are allowed indefinitely.
    • A limit of 0 means no refunds are allowed.
  • Task Objective:

    Process a list of commands to manage transaction intents and output the final balances of all accounts.

    Input Format:

    A time-ordered list of commands.

    Output Format:

    A list of account balances in ascending order by account ID.

    Examples
    01 · Example 1
    commands = ["START account1 0", "NEW txn1 account1 50", "PROCESS txn1", "COMPLETE txn1"]
    return = ["account1 50"]

    The commands are processed as follows:

    1. START account1 0: Initializes account1 with a balance of 0.
    2. NEW txn1 account1 50: Creates a new transaction intent txn1 for account1 with an amount of 50 in PENDING state.
    3. PROCESS txn1: Moves txn1 to IN_PROGRESS state.
    4. COMPLETE txn1: Moves txn1 to DONE state and adds the amount to account1's balance, resulting in a final balance of 50.
    The final balance for account1 is 50.

    02 · Example 2
    commands = ["START account1 0", "NEW txn1 account1 50", "PROCESS txn1", "CANCEL txn1", "PROCESS txn1", "COMPLETE txn1", "RETURN txn1"]
    return = ["account1 0"]

    The commands are processed as follows:

    1. START account1 0: Initializes account1 with a balance of 0.
    2. NEW txn1 account1 50: Creates a new transaction intent txn1 for account1 with an amount of 50 in PENDING state.
    3. PROCESS txn1: Moves txn1 to IN_PROGRESS state.
    4. CANCEL txn1: Reverts txn1 to PENDING state.
    5. PROCESS txn1: Moves txn1 back to IN_PROGRESS state.
    6. COMPLETE txn1: Moves txn1 to DONE state and adds the amount to account1's balance, resulting in a balance of 50.
    7. RETURN txn1: Processes a refund for txn1 and deducts the amount from account1's balance, resulting in a final balance of 0.
    The final balance for account1 is 0.

    03 · Example 3
    commands = ["1 START account1 0 5", "2 NEW txn1 account1 100", "8 COMPLETE txn1", "11 RETURN txn1", "16 RETURN txn1"]
    return = ["account1 100"]

    The commands are processed as follows:

    1. 1 START account1 0 5: Initializes account1 with a balance of 0 and a refund limit of 5.
    2. 2 NEW txn1 account1 100: Creates a new transaction intent txn1 for account1 with an amount of 100 in PENDING state.
    3. 8 COMPLETE txn1: Moves txn1 to DONE state and adds the amount to account1's balance, resulting in a balance of 100.
    4. 11 RETURN txn1: Processes a refund for txn1 within the refund limit and deducts the amount from account1's balance, resulting in a balance of 0.
    5. 16 RETURN txn1: The refund is not processed because it is outside the refund limit, so the balance remains 100.
    The final balance for account1 is 100.

    More Stripe problems
    drafts saved locally
    public String[] processCommands(String[] commands) {
        // write your code here
    }
    
    commands["START account1 0", "NEW txn1 account1 50", "PROCESS txn1", "COMPLETE txn1"]
    expected["account1 50"]
    sign in to submit