Design a code review workflow state machine with a required reviewer
Quick Overview
Implement a small finite state machine for a code review workflow with Pending, Approved and Changes Requested states, a required reviewer, and a reset operation. It tests guarded transitions, input validation, exact return values on failure, and how the design would change as more workflow rules are added.
Design a code review workflow state machine with a required reviewer
Company: Linear
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a `ReviewWorkflow` class that models a finite state machine for a code review with three possible states: `Pending`, `Approved`, and `Changes Requested`.
The workflow starts in the `Pending` state. Before transitioning to `Approved` or `Changes Requested`, a valid reviewer ID must be set via `setReviewer`. A valid reviewer ID is a non-empty string; setting an empty or null reviewer is an error.
Calling `approve` or `requestChanges` without a reviewer set is also an error. Errors are reported through the return value: the method returns the error message listed below and leaves the state and the reviewer unchanged. The `reset` method returns the workflow to `Pending` and clears the reviewer. The `getState` method returns the current state, and `getReviewer` returns the currently assigned reviewer (or an empty string if none).
Having a reviewer is the only guard. While a reviewer is set, `approve` and `requestChanges` succeed from any state, including a move between `Approved` and `Changes Requested`, and `setReviewer` may replace the current reviewer at any time.
Implement the following methods:
- `setReviewer(reviewerId: str) -> str` sets the reviewer ID. It returns `"OK"` on success, or `"Error: reviewer ID must be a non-empty string"` if the ID is empty or null.
- `approve() -> str` moves the state to `Approved`. It returns `"OK"`, or `"Error: cannot approve without a reviewer"` if no reviewer is set.
- `requestChanges() -> str` moves the state to `Changes Requested`. It returns `"OK"`, or `"Error: cannot request changes without a reviewer"` if no reviewer is set.
- `reset() -> str` returns the workflow to `Pending`, clears the reviewer, and returns `"OK"`.
- `getState() -> str` returns the current state: `"Pending"`, `"Approved"` or `"Changes Requested"`.
- `getReviewer() -> str` returns the current reviewer ID, or an empty string if none has been set.
A successful approval followed by a reset:
```text
wf = ReviewWorkflow() # state "Pending", reviewer ""
wf.getState() -> "Pending"
wf.setReviewer("alice") -> "OK" # reviewer set
wf.approve() -> "OK" # state "Approved"
wf.getState() -> "Approved"
wf.getReviewer() -> "alice"
wf.reset() -> "OK" # state "Pending", reviewer cleared
wf.getState() -> "Pending"
wf.getReviewer() -> ""
```
Once alice is the reviewer, `approve` succeeds, and `reset` puts both fields back to their starting values.
Rejected calls before a reviewer exists:
```text
wf = ReviewWorkflow() # state "Pending"
wf.approve() -> "Error: cannot approve without a reviewer"
wf.requestChanges() -> "Error: cannot request changes without a reviewer"
wf.setReviewer("") -> "Error: reviewer ID must be a non-empty string"
wf.setReviewer("bob") -> "OK"
wf.requestChanges() -> "OK" # state "Changes Requested"
wf.getState() -> "Changes Requested"
```
Both transitions fail while no reviewer is set, and the empty ID is rejected without assigning anything. After bob is assigned, `requestChanges` succeeds.
```hint One guard, no side effects
Both transitions check the same precondition. Put that check in one place, and make sure a rejected call leaves the object exactly as it found it.
```
### Constraints & Assumptions
- Reviewer IDs are non-empty strings.
- The initial state is `Pending` with no reviewer assigned.
- Every operation runs in `O(1)` time.
### Clarifying Questions to Ask
- Is a reviewer ID made only of spaces valid, or should IDs be trimmed before the check?
- The examples show error strings. In a real codebase, would exceptions be acceptable instead?
- Does the workflow need a history of who moved it to each state and when?
- Are more states or rules likely later, such as several required approvals?
### What a Strong Answer Covers
- How the states are represented, and how the class keeps an invalid state value from ever being stored.
- The reviewer guard on both transitions, and the guarantee that a rejected call changes nothing.
- Validation of the reviewer ID, including empty and null input, and what happens to an existing reviewer when a bad ID arrives.
- The exact return values and error messages, and what `reset` restores.
- How the design would change if more states or transition rules were added.
### Follow-up Questions
- How would you change the design so that an approved review is locked and only `reset` can leave `Approved`?
- A review now needs approvals from two different reviewers before it becomes `Approved`. What changes in the data model and the transitions?
- How would you record an audit trail of every transition and who made it?
- How would you keep the workflow consistent if two reviewers act on it at the same time?
Overview: Implement a small finite state machine for a code review workflow with Pending, Approved and Changes Requested states, a required reviewer, and a reset operation. It tests guarded transitions, input validation, exact return values on failure, and how the design would change as more workflow rules are added.
Design a ReviewWorkflow class that models a finite state machine for a code review with three possible states: Pending, Approved, and Changes Requested.
The workflow starts in the Pending state. Before transitioning to Approved or Changes Requested, a valid reviewer ID must be set via setReviewer. A valid reviewer ID is a non-empty string; setting an empty or null reviewer is an error.
Calling approve or requestChanges without a reviewer set is also an error. Errors are reported through the return value: the method returns the error message listed below and leaves the state and the reviewer unchanged. The reset method returns the workflow to Pending and clears the reviewer. The getState method returns the current state, and getReviewer returns the currently assigned reviewer (or an empty string if none).
Having a reviewer is the only guard. While a reviewer is set, approve and requestChanges succeed from any state, including a move between Approved and Changes Requested, and setReviewer may replace the current reviewer at any time.
Implement the following methods:
setReviewer(reviewerId: str) -> str
sets the reviewer ID. It returns
"OK"
on success, or
"Error: reviewer ID must be a non-empty string"
if the ID is empty or null.
approve() -> str
moves the state to
Approved
. It returns
"OK"
, or
"Error: cannot approve without a reviewer"
if no reviewer is set.
requestChanges() -> str
moves the state to
Changes Requested
. It returns
"OK"
, or
"Error: cannot request changes without a reviewer"
if no reviewer is set.
reset() -> str
returns the workflow to
Pending
, clears the reviewer, and returns
"OK"
.
getState() -> str
returns the current state:
"Pending"
,
"Approved"
or
"Changes Requested"
.
getReviewer() -> str
returns the current reviewer ID, or an empty string if none has been set.
Once alice is the reviewer, approve succeeds, and reset puts both fields back to their starting values.
Rejected calls before a reviewer exists:
wf = ReviewWorkflow() # state "Pending"
wf.approve() -> "Error: cannot approve without a reviewer"
wf.requestChanges() -> "Error: cannot request changes without a reviewer"
wf.setReviewer("") -> "Error: reviewer ID must be a non-empty string"
wf.setReviewer("bob") -> "OK"
wf.requestChanges() -> "OK" # state "Changes Requested"
wf.getState() -> "Changes Requested"
Both transitions fail while no reviewer is set, and the empty ID is rejected without assigning anything. After bob is assigned, requestChanges succeeds.
Constraints & Assumptions
Reviewer IDs are non-empty strings.
The initial state is
Pending
with no reviewer assigned.
Every operation runs in
O(1)
time.
Clarifying Questions to Ask Guidance
Is a reviewer ID made only of spaces valid, or should IDs be trimmed before the check?
The examples show error strings. In a real codebase, would exceptions be acceptable instead?
Does the workflow need a history of who moved it to each state and when?
Are more states or rules likely later, such as several required approvals?
What a Strong Answer Covers Guidance
How the states are represented, and how the class keeps an invalid state value from ever being stored.
The reviewer guard on both transitions, and the guarantee that a rejected call changes nothing.
Validation of the reviewer ID, including empty and null input, and what happens to an existing reviewer when a bad ID arrives.
The exact return values and error messages, and what
reset
restores.
How the design would change if more states or transition rules were added.
Follow-up Questions Guidance
How would you change the design so that an approved review is locked and only
reset
can leave
Approved
?
A review now needs approvals from two different reviewers before it becomes
Approved
. What changes in the data model and the transitions?
How would you record an audit trail of every transition and who made it?
How would you keep the workflow consistent if two reviewers act on it at the same time?