Problem, as an interviewer would state it:
"Build a minimal finite state machine runner: given a config of states and their allowed transitions, send(event) should move to the next state — but only if an optional GUARD function, checking real context data, allows it."
Examples:
const machine = createMachine({ initial: "red", states: {...} });
machine.send("TIMER"); // transitions if a matching, allowed transition exists
Clarifying questions expected:
- What should happen when
sendis called with an event that has NO matching transition from the current state? - Should a guard function receive the machine's own context data, to make a real, informed decision?
- Does the machine need to support entry/exit actions (side effects run on entering/leaving a state), or just the pure transition logic?
Code / implementation expected: Yes — real, direct proof through a real, multi-step traffic-light sequence: correct transitions on valid events, a guard genuinely blocking one specific transition, and an unhandled event correctly leaving the state unchanged.