Process List of Commands
Updated
Problem statement
You are building a tool that tracks payment transactions for merchant accounts and computes each account's final balance. A transaction is always in one of these states:
PENDING: the transaction is created and waiting to be processed.IN_PROGRESS: the transaction is being processed.DONE: the transaction is completed and its amount was added to the balance.REFUNDED: the transaction was refunded and its amount was taken back out of the balance.
Core commands:
START <account_id> <initial_balance> [refund_limit]: creates a merchant account with the given balance. The refund limit is optional.NEW <transaction_id> <account_id> <amount>: creates a transaction for the account with the given amount, in thePENDINGstate.PROCESS <transaction_id>: moves aPENDINGtransaction toIN_PROGRESS.COMPLETE <transaction_id>: moves anIN_PROGRESStransaction toDONEand adds its amount to the account's balance.
Extended commands:
MODIFY <transaction_id> <new_amount>: changes the amount of aPENDINGtransaction.CANCEL <transaction_id>: moves anIN_PROGRESStransaction back toPENDING.RETURN <transaction_id>: refunds aDONEtransaction: its amount is subtracted from the account's balance and it becomesREFUNDED.
If a transaction is not in the state a command needs (for example, COMPLETE on a PENDING transaction), the command does nothing. A REFUNDED transaction never changes again.
Time-limited refunds:
- Commands may start with a time, for example
8 COMPLETE txn1. Either every command has a time or none does. - If an account has a refund limit
L, aRETURNat timetworks only ift - c ≤ L, wherecis the time of theCOMPLETEcommand that made the transactionDONE. Otherwise theRETURNdoes nothing and the transaction staysDONE. - If an account has no refund limit, refunds are always allowed. A limit of
0means no refunds are allowed (times strictly increase, sot - cis always at least 1).
Process the commands in order and return the final balance of every account as the string "<account_id> <balance>", sorted by account ID in dictionary order (compare the IDs character by character, so "account10" comes before "account2").
Complete the function processCommands in the editor.
processCommands has the following parameter:
String[] commands: the commands, in time order
The function returns a String[]: one "<account_id> <balance>" string per account, sorted by account ID.
Function
processCommands(commands: String[]) → String[]Examples
Example 1
commands = ["START account1 0", "NEW txn1 account1 50", "PROCESS txn1", "COMPLETE txn1"]return = ["account1 50"]The commands are processed as follows:
START account1 0initializesaccount1with a balance of 0.NEW txn1 account1 50createstxn1with amount 50 in thePENDINGstate.PROCESS txn1moves the transaction toIN_PROGRESS.COMPLETE txn1moves the transaction toDONEand adds 50 to the account balance.
The final balance is 50.
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:
START account1 0initializesaccount1with a balance of 0.NEW txn1 account1 50createstxn1with amount 50 in thePENDINGstate.PROCESS txn1moves the transaction toIN_PROGRESS.CANCEL txn1moves the transaction back toPENDING.- The second
PROCESS txn1moves the transaction toIN_PROGRESSagain. COMPLETE txn1moves the transaction toDONEand adds 50 to the balance.RETURN txn1refunds the completed transaction and deducts 50.
The final balance is 0.
Example 3
commands = ["1 START account1 0 5", "2 NEW txn1 account1 100", "8 COMPLETE txn1", "11 RETURN txn1", "16 RETURN txn1"]return = ["account1 0"]The commands are processed as follows:
1 START account1 0 5initializesaccount1with balance 0 and a refund limit of 5.2 NEW txn1 account1 100createstxn1with amount 100 in thePENDINGstate.8 COMPLETE txn1has no effect becausetxn1is stillPENDING;COMPLETEonly moves a transaction fromIN_PROGRESStoDONE.11 RETURN txn1has no effect because only aDONEtransaction can be refunded.16 RETURN txn1has no effect for the same reason.
No command changes the account balance, so the final balance is 0.
Constraints
1 ≤ commands.length ≤ 10001 ≤ commands[i].length ≤ 60- Each command has one of the seven forms above, optionally preceded by a time, with single spaces between parts.
- Either every command starts with a time or none does. Times are integers from
1to109and strictly increase from one command to the next. - If the commands have no times, no
STARTcommand gives a refund limit. - Account IDs and transaction IDs have
1to10characters, each a lowercase English letter or a digit. - Each account is started once. Every
NEWuses a transaction ID not used before and an account created by an earlierSTART. - Every
PROCESS,COMPLETE,MODIFY,CANCELandRETURNnames a transaction created by an earlierNEW. 0 ≤ initial_balance ≤ 1061 ≤ amount, new_amount ≤ 1060 ≤ refund_limit ≤ 109- All numbers in commands are integers written in plain decimal form.
More Stripe interview questions
- Asynchronous Event ProcessingOA · Seen Nov 2026
- Business Account KYC Verification — Parts 1–5PHONE SCREEN · Seen Nov 2026
- Merchant Matching, Parts 1–2: Shared Fields and Weighted ThresholdsPHONE SCREEN · Seen Nov 2026
- Maximum Game Score with Fixed Forward JumpsOA · Seen Nov 2026
- Parse Boolean Rule ExpressionsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Reliable LLM Call with Independent Retry BudgetsOA · Seen Nov 2026
- Request Routing SystemOA · Seen Nov 2026
- Merchant Payout Scheduler, Part 2: Per-Merchant SchedulesPHONE SCREEN · Seen Nov 2026
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesPHONE SCREEN · Seen Nov 2026
- Financial Account LedgerOA · PHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Corporate Card Expense RulesRippling · OA · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Process Banking OperationsVercel · OA
- Expiring Authentication Token ManagerIBM · OAIBM interview questions
- Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
- Event Statistics AggregatorWells Fargo · OAWells Fargo interview questions
- Temporal Key-Field Database with Prefix ScansThe Trade Desk · OA
- Per-Client Sliding-Window Rate LimiterGeneral Motors · OA