Implement a state machine for a shared scooter's lifecycle

Quick Overview

Implement a finite state machine for a shared scooter with five states and a fixed set of allowed events. It checks table-driven design, rejecting invalid transitions without changing state, constant-time lookups, and how the design handles concurrency and side effects.

Implement a state machine for a shared scooter's lifecycle

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a state machine that manages the lifecycle of a shared scooter. The scooter can be in exactly one of five states at any time: `AVAILABLE`, `IN_USE`, `RESERVED`, `LOW_BATTERY`, or `MAINTENANCE`. The scooter starts in the `AVAILABLE` state. Each event attempts a transition; only the transitions listed below are valid. Any other event must be rejected as an invalid transition. - From `AVAILABLE`: `RESERVE` → `RESERVED`, `START_RIDE` → `IN_USE`, `REPORT_LOW_BATTERY` → `LOW_BATTERY`, `REPORT_DAMAGE` → `MAINTENANCE`. - From `RESERVED`: `START_RIDE` → `IN_USE`, `CANCEL_RESERVATION` → `AVAILABLE`, `RESERVATION_EXPIRED` → `AVAILABLE`. - From `IN_USE`: `END_RIDE` → `AVAILABLE`, `REPORT_LOW_BATTERY` → `LOW_BATTERY`, `REPORT_DAMAGE` → `MAINTENANCE`. - From `LOW_BATTERY`: `RECHARGE` → `AVAILABLE`, `REPORT_DAMAGE` → `MAINTENANCE`. - From `MAINTENANCE`: `REPAIR_COMPLETE` → `AVAILABLE`. On a valid event, transition to the new state and return the new state. On an invalid event for the current state, return the string `"INVALID_TRANSITION"` and leave the current state unchanged. Implement the following methods: - `transition(event: str) -> str` attempts to transition using the given event. It returns the new state on success, or `"INVALID_TRANSITION"` if the event is not allowed from the current state (the state is unchanged). - `getState() -> str` returns the current state of the scooter. Example 1: ```text Input: ["ScooterStateMachine", "getState", "transition", "transition", "getState", "transition", "transition"] [[], [], ["RESERVE"], ["START_RIDE"], [], ["END_RIDE"], ["RECHARGE"]] Output: [null, "AVAILABLE", "RESERVED", "IN_USE", "IN_USE", "AVAILABLE", "INVALID_TRANSITION"] Explanation: scooter = ScooterStateMachine() # starts in AVAILABLE scooter.getState() # → "AVAILABLE" scooter.transition("RESERVE") # → "RESERVED" (AVAILABLE → RESERVED) scooter.transition("START_RIDE") # → "IN_USE" (RESERVED → IN_USE) scooter.getState() # → "IN_USE" scooter.transition("END_RIDE") # → "AVAILABLE" (IN_USE → AVAILABLE) scooter.transition("RECHARGE") # → "INVALID_TRANSITION" (RECHARGE not valid from AVAILABLE) ``` Example 2: ```text Input: ["ScooterStateMachine", "transition", "transition", "getState", "transition", "transition", "getState"] [[], ["REPORT_LOW_BATTERY"], ["RECHARGE"], [], ["REPORT_DAMAGE"], ["REPAIR_COMPLETE"], []] Output: [null, "LOW_BATTERY", "AVAILABLE", "AVAILABLE", "MAINTENANCE", "AVAILABLE", "AVAILABLE"] Explanation: scooter = ScooterStateMachine() # starts in AVAILABLE scooter.transition("REPORT_LOW_BATTERY") # → "LOW_BATTERY" (AVAILABLE → LOW_BATTERY) scooter.transition("RECHARGE") # → "AVAILABLE" (LOW_BATTERY → AVAILABLE) scooter.getState() # → "AVAILABLE" scooter.transition("REPORT_DAMAGE") # → "MAINTENANCE" (AVAILABLE → MAINTENANCE) scooter.transition("REPAIR_COMPLETE") # → "AVAILABLE" (MAINTENANCE → AVAILABLE) scooter.getState() # → "AVAILABLE" ``` ```hint Where the rules live Think about representing the allowed transitions as data that you look up, so one lookup tells you both whether an event is allowed and where it leads. ``` ```hint Rejected events Check that the event is valid before you touch the current state. A rejected event must leave the scooter exactly where it was. ``` ### Constraints & Assumptions - The scooter always starts in state `AVAILABLE`. - The state is always one of `AVAILABLE`, `IN_USE`, `RESERVED`, `LOW_BATTERY`, `MAINTENANCE`. - Invalid events return the string `"INVALID_TRANSITION"` and leave the state unchanged. - At most ${10^4}$ calls combined to `transition` and `getState`. ### Clarifying Questions to Ask - Are event names matched exactly and case-sensitively, and should an event string that appears nowhere in the table be treated the same as a known event sent from the wrong state? - Will `transition` be called from several threads at once, or can I assume a single caller? - Is the transition table fixed, or should it be possible to pass in a different table later when new states or events are added? ### What a Strong Answer Covers - How the allowed transitions are represented, and why that representation is easy to audit and change. - How an invalid event is detected and rejected without changing the current state. - The time and space cost of each call. - Unknown event names, and known events sent from a state that does not allow them. - What changes in the code when a new state or event is added. ### Follow-up Questions - `RESERVATION_EXPIRED` has to come from somewhere. How would you fire it when a reservation times out, and what should happen if the rider starts the ride at the same moment? - Two riders try to reserve the same scooter at once. How do you make `transition` safe under concurrency, first in one process and then across several servers? - How would you attach side effects to transitions, such as starting billing on `START_RIDE` or alerting a technician on `REPORT_DAMAGE`? - How would you add a guard condition, for example refusing `START_RIDE` when the battery level is below a threshold?

Overview: Implement a finite state machine for a shared scooter with five states and a fixed set of allowed events. It checks table-driven design, rejecting invalid transitions without changing state, constant-time lookups, and how the design handles concurrency and side effects.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a state machine that manages the lifecycle of a shared scooter. The scooter can be in exactly one of five states at any time: AVAILABLE, IN_USE, RESERVED, LOW_BATTERY, or MAINTENANCE.

The scooter starts in the AVAILABLE state. Each event attempts a transition; only the transitions listed below are valid. Any other event must be rejected as an invalid transition.

  • From AVAILABLE : RESERVE → RESERVED , START_RIDE → IN_USE , REPORT_LOW_BATTERY → LOW_BATTERY , REPORT_DAMAGE → MAINTENANCE .
  • From RESERVED : START_RIDE → IN_USE , CANCEL_RESERVATION → AVAILABLE , RESERVATION_EXPIRED → AVAILABLE .
  • From IN_USE : END_RIDE → AVAILABLE , REPORT_LOW_BATTERY → LOW_BATTERY , REPORT_DAMAGE → MAINTENANCE .
  • From LOW_BATTERY : RECHARGE → AVAILABLE , REPORT_DAMAGE → MAINTENANCE .
  • From MAINTENANCE : REPAIR_COMPLETE → AVAILABLE .

On a valid event, transition to the new state and return the new state. On an invalid event for the current state, return the string "INVALID_TRANSITION" and leave the current state unchanged.

Implement the following methods:

  • transition(event: str) -> str attempts to transition using the given event. It returns the new state on success, or "INVALID_TRANSITION" if the event is not allowed from the current state (the state is unchanged).
  • getState() -> str returns the current state of the scooter.

Example 1:

Input:
["ScooterStateMachine", "getState", "transition", "transition",   "getState", "transition", "transition"]
[[],                    [],         ["RESERVE"],  ["START_RIDE"], [],         ["END_RIDE"], ["RECHARGE"]]

Output: [null, "AVAILABLE", "RESERVED", "IN_USE", "IN_USE", "AVAILABLE", "INVALID_TRANSITION"]

Explanation:
scooter = ScooterStateMachine()           # starts in AVAILABLE
scooter.getState()                        # → "AVAILABLE"
scooter.transition("RESERVE")             # → "RESERVED"           (AVAILABLE → RESERVED)
scooter.transition("START_RIDE")          # → "IN_USE"             (RESERVED → IN_USE)
scooter.getState()                        # → "IN_USE"
scooter.transition("END_RIDE")            # → "AVAILABLE"          (IN_USE → AVAILABLE)
scooter.transition("RECHARGE")            # → "INVALID_TRANSITION" (RECHARGE not valid from AVAILABLE)

Example 2:

Input:
["ScooterStateMachine", "transition",           "transition", "getState", "transition",      "transition",        "getState"]
[[],                    ["REPORT_LOW_BATTERY"], ["RECHARGE"], [],         ["REPORT_DAMAGE"], ["REPAIR_COMPLETE"], []]

Output: [null, "LOW_BATTERY", "AVAILABLE", "AVAILABLE", "MAINTENANCE", "AVAILABLE", "AVAILABLE"]

Explanation:
scooter = ScooterStateMachine()           # starts in AVAILABLE
scooter.transition("REPORT_LOW_BATTERY")  # → "LOW_BATTERY"        (AVAILABLE → LOW_BATTERY)
scooter.transition("RECHARGE")            # → "AVAILABLE"          (LOW_BATTERY → AVAILABLE)
scooter.getState()                        # → "AVAILABLE"
scooter.transition("REPORT_DAMAGE")       # → "MAINTENANCE"        (AVAILABLE → MAINTENANCE)
scooter.transition("REPAIR_COMPLETE")     # → "AVAILABLE"          (MAINTENANCE → AVAILABLE)
scooter.getState()                        # → "AVAILABLE"

Constraints & Assumptions

  • The scooter always starts in state AVAILABLE .
  • The state is always one of AVAILABLE , IN_USE , RESERVED , LOW_BATTERY , MAINTENANCE .
  • Invalid events return the string "INVALID_TRANSITION" and leave the state unchanged.
  • At most 104{10^4} calls combined to transition and getState .

Clarifying Questions to Ask Guidance

  • Are event names matched exactly and case-sensitively, and should an event string that appears nowhere in the table be treated the same as a known event sent from the wrong state?
  • Will transition be called from several threads at once, or can I assume a single caller?
  • Is the transition table fixed, or should it be possible to pass in a different table later when new states or events are added?

What a Strong Answer Covers Guidance

  • How the allowed transitions are represented, and why that representation is easy to audit and change.
  • How an invalid event is detected and rejected without changing the current state.
  • The time and space cost of each call.
  • Unknown event names, and known events sent from a state that does not allow them.
  • What changes in the code when a new state or event is added.

Follow-up Questions Guidance

  • RESERVATION_EXPIRED has to come from somewhere. How would you fire it when a reservation times out, and what should happen if the rider starts the ride at the same moment?
  • Two riders try to reserve the same scooter at once. How do you make transition safe under concurrency, first in one process and then across several servers?
  • How would you attach side effects to transitions, such as starting billing on START_RIDE or alerting a technician on REPORT_DAMAGE ?
  • How would you add a guard condition, for example refusing START_RIDE when the battery level is below a threshold?