For All Intents And Purposes Part 3 - Accepting Failure π
Updated
Problem statement
π Hi there! The description you are currently reading is just 3rd part of the problem set. It is highly recommended to read ALL THE 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 β°
The rest of the team is thrilled with our progress building Stripe's new Payment Intent processor, but they pointed out a few cases we should handle before releasing the system to the public π₯
First, payments don't alway succeed and can fail for a variety of reasons like a card network declining a transaction or a bank account not having sufficient funds to be debited. Second, even after payments have succeeded, customers need to be able to request refunds from merchant.
Let's update our implementation to handle two new commands: FAIL and REFUND π₯Ή.
All commands from Parts 1 and 2 (INIT, CREATE, ATTEMPT, SUCCEED and UPDATE) still work exactly as described there. The output is also the same as in Part 1: one string "<merchant_id> <balance>" for every initialized merchant, sorted by merchant ID. IDs are compared as plain strings, character by character (so m10 comes before m2).
Commands
FAIL <payment_intent_id>
- Fails a payment intent in the PROCESSING state, transitioning it from PROCESSING to REQUIRES_ACTION. This occurs when the customer's payment was declined.
- If no Payment Intent with the given identifier exists, or if the Payment Intent is not in the PROCESSING state, this command should do nothing.
REFUND <payment_intent_id>
- Processes a refund for a previously successful payment intent in the COMPLETED state. This should decrement the merchant's balance by the amount of the Payment Intent in order to return the refund to the customer
- If no Payment Intent with the given identifier exists, or if the Payment Intent is not in the COMPLETED state, or if the Payment Intent has already been refunded, this command should do nothing.
Function
forAllIntentsAndPurposes3(commands: String[]) β String[]Examples
Example 1
commands = ["INIT m1 0", "CREATE p1 m1 50", "ATTEMPT p1", "FAIL p1", "ATTEMPT p1", "SUCCEED p1", "CREATE p2 m1 100", "ATTEMPT p2", "SUCCEED p2", "REFUND p2"]return = ["m1 50"]The Payment Intent p1 was created with an amount of 50, then attempted, then failed which transitioned it back to the REQUIRES_ACTION, then attempted, then succeeded. At that point, m1's balance was 50. The Payment Intent p2 was created with a amount of 100, then attempted, then succeeded. At that point, m1's balance was 150. Then p2 was refunded, so m1's balance after executing all commands is 50 :)
Constraints
1 β€ commands.length β€ 10^4- Each command is one of
INIT,CREATE,ATTEMPT,SUCCEED,UPDATE,FAILorREFUND, written exactly as described, with single spaces between its parts. - Merchant IDs and Payment Intent IDs have
1to10characters, and every character is a lowercase English letter or a digit. 0 β€ starting_balance β€ 10^5-10^5 β€ amount, new amount β€ 10^5- Every number is a plain decimal integer (a minus sign only for negative numbers, no leading zeros).
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
- Student and Course EnrollmentWells Fargo Β· OAWells Fargo interview questions
- Overheat Prevention ControllerOptiver Β· OAOptiver interview questions
- LFU CacheSnowflake Β· OA Β· ONSITE INTERVIEWSnowflake 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 Attribute Store with TTLAirbnb Β· OAAirbnb interview questions
- Process Banking OperationsVercel Β· OA
- Four-Level Banking System with Scheduled PaymentsZipRecruiter Β· OAZipRecruiter interview questions
- Expiring Authentication Token ManagerIBM Β· OAIBM interview questions
- LFU CacheArista Networks Β· OAArista Networks interview questions