InterviewDB Experience

Engine States: Model a Vehicle Engine Using a Finite State Machine

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

  1. How would you add entry/exit callbacks for each state (e.g., log when entering ERROR)?
  2. How do you serialize and restore the FSM's current state from persistent storage?
  3. How would you extend this to a hierarchical FSM where RUNNING has sub-states like ACCELERATING and CRUISING?
  4. 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

  1. How would you add entry/exit callbacks for each state (e.g., log when entering ERROR)?
  2. How do you serialize and restore the FSM's current state from persistent storage?
  3. How would you extend this to a hierarchical FSM where RUNNING has sub-states like ACCELERATING and CRUISING?
  4. How would you unit-test that all invalid transitions correctly raise errors?

About This Question

This is a candidate experience report from a samsara interview during the phone round.

It covers the following topics: Coding, Ood, Phone .