Data Structures & Algorithms · Interview question

What is a finite state machine and why is it the right tool for protocol parsing?

A strong answer

An FSM is a model where the system is always in exactly one of a finite set of states and moves between them via transitions triggered by events, optionally producing actions. It's the right tool for parsing a byte stream because you receive input incrementally, one byte per UART interrupt, and can't block waiting for a whole frame; the FSM lets you process each byte as an event and remember where you are in the protocol (waiting for sync, reading length, collecting payload, checking the checksum) in the current state. Each byte triggers at most one transition, so it's O(1) per byte and O(states) memory, with no buffering of the entire frame required beyond the payload you're accumulating. The state is the parser's memory of partial progress, which is exactly what stream parsing needs.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Finite State Machines

One state at a time, transitions on events: the structure behind protocol parsers, button debouncers, and comms stacks, implemented as a switch or a transition table.

More Finite State Machines questions

Browse all 472 interview questions