InterviewDB
Experience
Engine States: Model a Vehicle Engine Using a Finite State Machine
phone
Interview Experience
Round 1 Coding / OOD
Problem
Model a vehicle engine's lifecycle using a finite state machine. The engine has states: OFF, STARTING, RUNNING, IDLE, ERROR. Design the transition logic and make illegal transitions raise an error.
python
class EngineStateMachine:
TRANSITIONS = {
"OFF": {"start": "STARTING"},
"STARTING": {"started": "RUNNING", "fail": "ERROR"},
"RUNNING": {"idle": "IDLE", "stop": "OFF"},
"IDLE": {"revive": "RUNNING", "stop": "OFF"},
"ERROR": {"reset": "OFF"},
}
def __init__(self):
self.state = "OFF"
def trigger(self, event: str) -> str:
**returns** new state or raises InvalidTransitionError
...
def current_state(self) -> str:
...
Example
engine = EngineStateMachine()
engine.trigger("start") -> "STARTING"
engine.trigger("started") -> "RUNNING"
engine.trigger("idle") -> "IDLE"
engine.trigger("stop") -> "OFF"
engine.trigger("started") -> raises InvalidTransitionError
Follow-ups
- How would you add entry/exit callbacks for each state (e.g., log when entering ERROR)?
- How do you serialize and restore the FSM's current state from persistent storage?
- How would you extend this to a hierarchical FSM where RUNNING has sub-states like
ACCELERATINGandCRUISING? - How would you unit-test that all invalid transitions correctly raise errors?
Full Details
Round 1 Coding / OOD
Problem
Model a vehicle engine's lifecycle using a finite state machine. The engine has states: OFF, STARTING, RUNNING, IDLE, ERROR. Design the transition logic and make illegal transitions raise an error.
python
class EngineStateMachine:
TRANSITIONS = {
"OFF": {"start": "STARTING"},
"STARTING": {"started": "RUNNING", "fail": "ERROR"},
"RUNNING": {"idle": "IDLE", "stop": "OFF"},
"IDLE": {"revive": "RUNNING", "stop": "OFF"},
"ERROR": {"reset": "OFF"},
}
def __init__(self):
self.state = "OFF"
def trigger(self, event: str) -> str:
**returns** new state or raises InvalidTransitionError
...
def current_state(self) -> str:
...
Example
engine = EngineStateMachine()
engine.trigger("start") -> "STARTING"
engine.trigger("started") -> "RUNNING"
engine.trigger("idle") -> "IDLE"
engine.trigger("stop") -> "OFF"
engine.trigger("started") -> raises InvalidTransitionError
Follow-ups
- How would you add entry/exit callbacks for each state (e.g., log when entering ERROR)?
- How do you serialize and restore the FSM's current state from persistent storage?
- How would you extend this to a hierarchical FSM where RUNNING has sub-states like
ACCELERATINGandCRUISING? - How would you unit-test that all invalid transitions correctly raise errors?
Free preview. Unlock all Samsara questions →
About This Question
This is a candidate experience report from a samsara interview during the phone round.
More Samsara Interview Questions
InterviewDB
Samsara SWE Phone - Axis Permutation
InterviewDB
Command Parser: Parse and Dispatch Shell-Like Commands from User Input
InterviewDB
Samsara SWE Phone - Concurrent Meetings
InterviewDB
Engine Utilization: Compute and Report Resource Utilization Across Processing Engines
InterviewDB
Samsara SWE Onsite - Intervals Merging