For All Intents And Purposes Part 4 - Timing Matters β°
Updated
Problem statement
π Hi there! The description you are currently reading is just 4th part of the problem set. It is highly recommended to read ALL THE 4 PARTS before coding as parts may build on top of each other π³
- For All Intents And Purposes Part 1 - Initilizing the System ποΈ
- For All Intents And Purposes Part 2 - Change Is Good! π
- For All Intents And Purposes Part 3 - Accepting Failure π
- For All Intents And Purposes Part 4 - Timing Matters β°
Last month, we received the new Payment Intent system you built π, and merchants have been LOVING how simple it makes accepting payments. Congratulations π!
However, numerous merchants have requested that we augment our refund functionality by letting merchants specify a refund timeout policy: the amount of time after a payment succeeds that refunds are permitted. For example, one merchant, LlmaCorp, only wants to accept refunds within 15 days of a payment succeeding. Being able to return a llama afte 15 days would be absurd after all.
Let's add this functionality to our system, which means we will need to change the format of our commands to handle timing πͺ.
Timestamps
- All commands will now have an integer timestamp in front. For example, intead of SUCCEED <pi_id>, you will now receive<timestamp>SUCCEED <pi_id>
- You can assume that the timestamps will always be integers (representing a unit of time) and that the timestamps of the list of commands will be strictly increasing
Merchant Refund Timeout Limit
- The INIT command that initializes a merchant will now have an optional third argument specifying the merchant's refund timeout limit. The new specification is -
π <timestamp> INIT <merchant_id> <starting_balance> <refund_time_out_limit>
- If a merchant has a refund_timeout_limit of n, and succeeds a Payment Intent (with the SUCCEED command) at timestamp t, then the timestamp of a REFUND command must be no greater than t + n, otherwise the refund should no succeed.
- iF A refund_timeout_limit is not specified during merchant initialization, there is no refund time limit, a a refund should succeed regardless how much time has passed since Payment Intent confirmation. A reund_timeout_limit of 0 means no refunds should be accepted ever, and a negative refund_timeout_limit should be ignore (meaning all refunds should be accepted)
Inherited payment-intent commands
This timed part retains the preceding command rules. Every command below has a strictly increasing integer timestamp prefix.
INIT merchant_id starting_balance [refund_timeout_limit]creates a merchant only when its ID does not exist.CREATE intent_id merchant_id amountcreates a new intent inREQUIRES_ACTIONonly when the intent ID is unused, the merchant exists, and the amount is nonnegative.UPDATE intent_id new_amountchanges a nonnegative amount only inREQUIRES_ACTION, without changing the state.ATTEMPT intent_idchangesREQUIRES_ACTIONtoPROCESSING.SUCCEED intent_idchangesPROCESSINGtoCOMPLETED, records that command's timestamp, and credits the merchant once.FAIL intent_idchangesPROCESSINGback toREQUIRES_ACTION, allowing an update or retry.REFUND intent_iddebits a completed, previously unrefunded intent once when the merchant's timeout permits it. A rejected refund changes nothing. A successful refund retainsCOMPLETEDand records a separate refunded flag.
Commands with missing identities or invalid state transitions do nothing. Return one string merchant_id balance for every initialized merchant in ascending lexicographic ID order. The graded method is forAllIntentsAndPurposes4(commands); it always processes timed Part 4 commands.
Bounds
For this exercise, assume IDs contain ASCII letters, digits, or underscores and have 1β50 characters; each command uses its documented syntax with single ASCII spaces. There are 0β1,000 commands and at most 500 distinct initialized merchants. Timestamps are integers in 0β1,000,000,000 and strictly increase. Starting balances, amounts, updated amounts, and specified timeout limits are integers in -1,000,000,000β1,000,000,000. Numeric tokens use canonical decimal notation: 0, a nonzero-leading sequence of decimal digits, or a minus sign followed by a nonzero-leading sequence for a negative number. Leading zeros, plus signs, and -0 are not valid input. Negative amounts remain valid command arguments but are rejected by CREATE or UPDATE. Accumulated balances require signed 64-bit arithmetic.
Ungraded follow-up: one implementation for all four parts
A variation uses a command list plus a part selector. Use processPaymentPart(commands, part), with part from 1 to 4, to expose that variation without changing the graded method. Parts 1β3 omit timestamp prefixes. Part 1 permits INIT, CREATE, ATTEMPT, and SUCCEED; Part 2 adds UPDATE; Part 3 adds FAIL and REFUND with no timeout; Part 4 adds the timestamp and merchant policy. Each invocation starts a fresh system and returns the same sorted final-balance format. All bounds, including the maximum of 500 distinct initialized merchants, apply to every part. Its input commands use only that part's documented operations. Complete implementations are included in both editorial approaches.
Function
forAllIntentsAndPurposes4(commands: String[]) β String[]Examples
Example 1
commands = ["1 INIT m1 0 5", "2 CREATE p1 m1 100", "3 CREATE p2 m1 50", "4 ATTEMPT p1", "5 ATTEMPT p2", "8 SUCCEED p1", "10 SUCCEED p2", "11 REFUND p1", "16 REFUND p2"]return = ["m1 50"]In the above example, the reund of Payment Intent p1 succeeded because p1 was succeeded at time 8 and refunded at time 11, which is within merchant m1's refund timeout limit of 5. However, the refund of Payment Intent p2 failed because p2 was succeeded at time 10 and refunded at time 16, and 16 - 10 = 6 > 5, meaning it occured after m1's timeout limit. Because p1 was successfully refunded and p2 was not, the final balance of merchant m1 should be 50 :P
Constraints
- For this exercise, assume 0β1,000 syntactically valid commands with the documented operation forms.
- For this exercise, assume at most 500 distinct initialized merchants; the same bound applies to every ungraded helper mode.
- IDs use 1β50 ASCII letters, digits, or underscores. Each command uses single ASCII spaces.
- Numeric tokens use canonical decimal notation: 0 or nonzero-leading decimal digits, with a minus sign only for negative numbers; no leading zeros, plus signs, or -0.
- Timed command timestamps strictly increase and are between 0 and 1,000,000,000.
- Starting balances, amount arguments, and timeout limits are between -1,000,000,000 and 1,000,000,000. Negative CREATE/UPDATE amounts are ignored.
- Use signed 64-bit arithmetic for aggregate merchant balances.
More Stripe interview questions
- Merchant Payout Scheduler, Part 2: Per-Merchant SchedulesPHONE SCREEN Β· Seen Nov 2026
- Financial Account LedgerOA Β· PHONE SCREEN Β· Seen Nov 2026
- Business Account KYC Verification β Parts 1β5PHONE SCREEN Β· Seen Nov 2026
- Chat Billing CalculationOA Β· Seen Nov 2026
- Render a Task Tree from CSV RowsPHONE SCREEN Β· ONSITE INTERVIEW Β· Seen Nov 2026
- Deployment Window SchedulerOA Β· Seen Nov 2026
- Invoice / Payment ReconciliationPHONE SCREEN Β· ONSITE INTERVIEW Β· Seen Nov 2026
- Linked Merchants Within Two HopsPHONE SCREEN Β· Seen Nov 2026
- Asynchronous Event ProcessingOA Β· Seen Nov 2026
- Portfolio Valuation, Part 1PHONE SCREEN Β· Seen Nov 2026
Similar questions at other companies
- 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
- Stock Marking Position MonitorAkuna Capital Β· OAAkuna Capital interview questions