Order State Machine with Transition Validation
Data Structure DesignAuto-graded · 15 test casesLast 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 isACTIVE"resume"succeeds only when the current state isPAUSED"cancel"succeeds only when the current state isACTIVEorPAUSED- Once an order reaches
CANCELLEDorFILLED, it stays there permanently—all subsequent operations fail - Orders starting in
NEWreject 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_stateis one of:"NEW","ACTIVE","PAUSED","CANCELLED","FILLED"1 ≤ len(operations) ≤ 1000- Each operation is
"pause","resume", or"cancel" len(operations) == len(arguments)argumentscontains 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires implementing a finite state machine with validation. The key insight is that each operation only succeeds under specific state conditions, and failed operations must not alter the current state.
We maintain a single state variable that tracks the order's current position in its lifecycle. For each operation, we check whether the combination of operation type and current state matches one of the allowed transitions. If it does, we update the state and record success. If not, we record failure and leave the state unchanged.
The transition logic is straightforward:
"pause"requires the state to be exactlyACTIVE, then moves toPAUSED"resume"requires the state to be exactlyPAUSED, then moves toACTIVE"cancel"requires the state to be eitherACTIVEorPAUSED, then moves toCANCELLED
All other combinations fail, including:
- Any operation on
NEW(no valid transitions out via these operations) - Any operation on
CANCELLEDorFILLED(terminal states) - Operations on wrong states (like pausing an already-paused order)
Why this is correct:
The state variable always reflects the true state after all successful transitions. Since rejected operations never modify state, each operation sees an accurate view of where the order currently stands. Terminal states naturally trap all operations because none of the conditional branches match when the state is CANCELLED or FILLED.
The approach correctly handles sequences like pause-pause-resume from ACTIVE, yielding [True, False, True]: the first pause succeeds, the second fails because the state is now PAUSED, and resume succeeds because it finds the order in the correct PAUSED state.
Complexity:
- Time: O(n) where n is the number of operations—each operation requires constant-time string comparison and list append
- Space: O(n) for the output list; the state tracking uses O(1) additional space
Solution
from typing import List
def validate_order_transitions(initial_state: str, operations: List[str], arguments: List[List]) -> List[bool]:
current_state = initial_state
results = []
for operation, args in zip(operations, arguments):
if operation == "pause" and current_state == "ACTIVE":
current_state = "PAUSED"
results.append(True)
elif operation == "resume" and current_state == "PAUSED":
current_state = "ACTIVE"
results.append(True)
elif operation == "cancel" and current_state in ("ACTIVE", "PAUSED"):
current_state = "CANCELLED"
results.append(True)
else:
results.append(False)
return results