← Questions

Payment intents: create, update, confirm, fail and refund

0
StripeCodingLast asked Apr 2025

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.

  1. Initialize merchants with a starting balance, create intents for a merchant and amount, attempt them and mark them succeeded; success credits the merchant.
  2. Allow updating an intent's amount while it still waits for action.
  3. Add failure (a processing intent goes back to waiting for action) and refunds of completed intents, which debit the merchant.
  4. 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

What it tests

state machines · design · hash map · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding