KatInterview

Payment Command Parser and Lifecycle State Machine Design

Data Structure DesignModel answer
HardModel answerData Structure Designstate-machineparsingobject-oriented-design

Last updated

Design a parser for fixed-arity payment commands and an object-oriented lifecycle state machine, and identify which business rules need clarification before implementation.

You are asked to design the core of a small command-driven payment module. Commands arrive as text lines. Every command has an operation token followed by exactly four argument positions. Positions that a command does not use are filled with an empty quoted string.

CREATE  id payer payee amount
APPROVE id "" "" amount
CAPTURE id "" "" amount
CANCEL  id "" "" ""

The partial lifecycle you were given is:

CREATED            -> APPROVED
APPROVED           -> PARTIALLY_CAPTURED or SUCCEEDED
PARTIALLY_CAPTURED -> SUCCEEDED

Cancellation is described as applying to payments that have not yet been approved. The description may be incomplete or partly wrong.

Ground rules

  • This is a design exercise over an incomplete specification. Do not invent undocumented behavior and present it as given.
  • Always keep the four argument positions, including empty ones.
  • Do not assume that the approved amount must equal the created amount, that capture amounts are increments rather than cumulative totals, or that repeated partial captures are allowed.
  • It is unclear whether CANCEL id is targeted or whether a bulk "cancel all unapproved payments" exists. Leave this open until clarified.
  • Money movement, balances, concurrency, currency format and output format are all unspecified. Define clean boundaries around these instead of guessing.

Part 1: Parsing. Describe a parser that preserves every argument position and produces a typed command object. Explain where syntax validation ends and payment-rule validation begins.

Part 2: States and policy boundaries. Design the payment objects and the command handler around the given transitions. List the missing rules you would need clarified before building a complete monetary state machine, and show how your design isolates those rules.

Also state the clarifying questions you would ask first.

Hints

Hint 1

Separate three layers: tokenizing text into fixed slots, converting slots into a typed command, and applying that command to a payment. Only the last layer may change state.

Hint 2

Represent the transition table as data in one place, keyed by current state and operation, so that unclear rules become small, replaceable policies instead of scattered if-statements.

Hint 3

Treat capture arithmetic, amount equality and cancel scope as injectable policies with a clearly documented default of 'reject as unspecified', and make the handler validate fully before it mutates anything.