Validate Payment Lifecycle Event Streams Against a State Machine
Last updated: November 29, 2026
Quick Overview
Payments move through states such as created, authorized, captured, failed, refunded and partially refunded. Given an interleaved stream of (payment_id, new_state) events, return for each payment the index of the first illegal transition, or None if its history is valid.
Razorpay
November 29, 20260
0
0 solved
Payments move through states such as created, authorized, captured, failed, refunded and partially refunded. Given an interleaved stream of (payment_id, new_state) events, return for each payment the index of the first illegal transition, or None if its history is valid.
Razorpay's core API models every payment as a strict state machine, and bugs in transitions mean double captures or refunds on failed payments. This online-assessment style task checks whether you can encode a transition table cleanly and process an interleaved stream with per-entity state.
What the Interviewer Expects
- Encode allowed transitions as data, not nested ifs
- Track current state per payment id
- Report the first bad event index and stop evaluating that payment
- Treat a missing initial created event as invalid
- Discuss terminal states
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Events can arrive out of order from Kafka. How would you handle that?
- How would you enforce this transition table inside the database rather than in application code?
- How do you model partial refunds whose sum must not exceed the captured amount?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
States: created -> authorized -> captured -> (partially_)refunded, with failed reachable from created or authorized, and an uncaptured authorization allowed to be refunded (auto-refund). failed ...
High-Level Design
A dict ALLOWED[prev_state] -> set(next_states) where None stands for not yet seen. Keep cur[pid] for the last valid state. Once a payment has an error, ignore its later events so the reported in...