Payment intents: create, update, confirm, fail and refund
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's HackerRank assessment, last asked Apr 2025. It tests state machines, design and hash map.
A payment intent tracks one payment from creation to completion as a small state machine: it waits for action, moves to processing when a payment is attempted, and becomes completed when it succeeds. Process a list of commands and print every merchant's final balance. Commands that don't fit an intent's current state, or that name unknown merchants or intents, do nothing.
- Initialize merchants with a starting balance, create intents for a merchant and amount, attempt them and mark them succeeded; success credits the merchant.
- Allow updating an intent's amount while it still waits for action.
- Add failure (a processing intent goes back to waiting for action) and refunds of completed intents, which debit the merchant.
- Commands now carry timestamps, and a merchant may set a refund window: refunds later than that after the payment succeeded are refused.
Clarifying questions
- What is the exact command syntax, and in what order should merchant balances be printed?
- What happens to a duplicate intent id, or an intent for an unknown merchant?
- Is a refund always the full amount, and only once?
- Is a refund exactly at the end of the window allowed? Are timestamps always increasing?
What a strong answer covers
- An intent with an explicit state (waiting for action, processing, completed, refunded) and one transition table; each command checks the current state and does nothing when the move is not allowed.
- Balances change only on success (credit) and on refund (debit); amount updates only while the intent waits for action.
- Part 4 stores when each intent succeeded and each merchant's refund window, and compares the refund's timestamp against both.
- Parsing kept apart from the state machine, so each new part adds commands without a rewrite.
- Tests: ignored moves, unknown ids, an update after an attempt, fail then retry, a refund on the window boundary.
Follow-ups
- Partial refunds, several of them, up to the amount.
- Disputes as another state.
- Idempotency keys on commands.
- Keep the commands as an event log and rebuild the balances from it.
Where it's asked
- Stripe · HackerRank assessment · Last asked Apr 2025Leveled HackerRank task; test cases map to the parts in order
What it tests
state machines · design · hash map · simulation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
