KatInterview

Order State Machine with Transition Validation

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designstate-machinevalidationdata-structures

Last updated

Tests finite state machine implementation with strict transition rules and rejection of invalid state changes.

You're building a component that tracks the lifecycle of an order through different states. Each order progresses through states like NEW, ACTIVE, PAUSED, CANCELLED, and FILLED.

Your task is to process a sequence of operations that attempt to change an order's state. Each operation must follow specific transition rules.

Write a function that takes an order's starting state and a list of operations, then returns which operations succeeded.

Operations:

  • "pause": Attempts to move an order from active trading to a paused state
  • "resume": Attempts to reactivate a paused order
  • "cancel": Attempts to permanently cancel an order

Transition Rules:

Your implementation must enforce these exact state transitions:

  • NEW → ACTIVE (not triggered by these operations)
  • ACTIVE → PAUSED (triggered by "pause")
  • PAUSED → ACTIVE (triggered by "resume")
  • ACTIVE → CANCELLED (triggered by "cancel")
  • PAUSED → CANCELLED (triggered by "cancel")
  • ACTIVE → FILLED (not triggered by these operations)

Important Constraints:

  • "pause" succeeds only when the current state is ACTIVE
  • "resume" succeeds only when the current state is PAUSED
  • "cancel" succeeds only when the current state is ACTIVE or PAUSED
  • Once an order reaches CANCELLED or FILLED, it stays there permanently—all subsequent operations fail
  • Orders starting in NEW reject all three operations (no valid transitions exist)
  • Operations that would create invalid transitions fail and leave the state unchanged
  • A failed operation does not prevent later operations from succeeding

Return a list of booleans indicating success (True) or failure (False) for each operation in sequence.

Constraints:

  • initial_state is one of: "NEW", "ACTIVE", "PAUSED", "CANCELLED", "FILLED"
  • 1 ≤ len(operations) ≤ 1000
  • Each operation is "pause", "resume", or "cancel"
  • len(operations) == len(arguments)
  • arguments contains metadata for each operation (can be ignored for this problem)

Examples

Example 1

Input: validate_order_transitions('ACTIVE', ['pause', 'resume', 'cancel'], [[], [], []])

Output: [True, True, True]

Example 2

Input: validate_order_transitions('PAUSED', ['resume', 'pause', 'cancel'], [[], [], []])

Output: [True, True, True]

Example 3

Input: validate_order_transitions('FILLED', ['cancel', 'pause'], [[], []])

Output: [False, False]

Hints

Hint 1

Think of this as a finite state machine where only specific operation-state pairs trigger transitions.

Hint 2

A rejected operation should never modify the current state—the order stays where it is.

Hint 3

Terminal states (CANCELLED and FILLED) act as traps: once reached, no operation can move the order out.