Interviews

State Machines Interview Questions

Practise the question, then the follow-ups an interviewer asks next.

Start with a Question TreeAI Mock Interview

3
Question trees
15
Follow-ups
22
Wrong answers explained
7
Runnable on a simulator
Question tree The interviewer asksThis machine detects two 1s in a row, with both a Mealy and a Moore output. Read the trace. When does each output rise, and why? WhyWhy a Mealy Output Arrives EarlyBreak ItTwo Mealy Machines Facing Each OtherCompareA Registered Mealy Output vs a Moore OutputWhat IfA Mealy Output Fed by a Late Input + 1 more follow-ups · Open this tree

Flagship Questions · With follow-ups

Question Trees

Each question opens into the follow-ups an interviewer asks next, the wrong answers people give, and what a fresher's and a senior's answer sound like.

Quick revision · One flat list

Question Bank

All 16 State Machines questions, main and follow-up. Open a track, then a topic. Answer out loud first, then open the short answer.

Jump to a Track
16 questions · 3 topicsState Machines
Moore vs Mealy Outputs on One Waveform5
This machine detects two 1s in a row, with both a Mealy and a Moore output. Read the trace. When does each output rise, and why? L0L1L2

The Mealy output rises in the cycle where the second 1 is on din. It depends on the state and the input together. The Moore output depends only on the state, so it rises one edge later, when the machine enters S2. Mealy is a cycle earlier, but its output follows the input inside the cycle. Moore changes only at clock edges.

QuestionPractise Moore vs Mealy Outputs on One Waveform →
Why is the Mealy output early, and what does that early output cost? L1L2

Its logic takes the current input, so it does not wait for the state to update at the next edge. The cost is a combinational path from the input pin to the output pin. That path adds to the timing of whatever drives the input and whatever reads the output.

Follow-upPractise Why a Mealy Output Arrives Early →
How can Mealy outputs break a design? Think of two blocks handshaking with valid and ready. L2L3

Suppose block A makes valid from ready, and block B makes ready from valid. The two Mealy paths then form a loop through both modules. Each module looks fine on its own. Synthesis or lint finds the loop only at the level above. Register one side, or follow the rule that valid must not wait for ready.

Follow-upPractise Two Mealy Machines Facing Each Other →
Compare registering the Mealy output with converting the machine to Moore. L2

Registering the Mealy output puts a flop after its logic. The output then changes only at edges, one cycle after the Mealy version, like a Moore output. It often needs fewer states than a Moore machine with the same behaviour. The output logic is computed from next-cycle information, so it can be decoded early.

Follow-upPractise A Registered Mealy Output vs a Moore Output →
What if the input to the Mealy output arrives late in the cycle, from another block's logic? L2

Then the Mealy output also settles late. The receiver has only what remains of the cycle. If the input glitches before it settles, the output glitches too. A receiver that samples at the edge does not care. A clock, a latch enable or another clock domain does.

Follow-upPractise A Mealy Output Fed by a Late Input →
Choosing a State Encoding6
Binary, one-hot or Gray: how do you choose a state encoding, and how do you check the tool used it? L1L2L3

Binary uses the fewest flops, about log2 of the state count, but needs decoding. One-hot uses one flop per state, so testing a state reads one bit. Gray changes one bit per step, which suits machines that move in a fixed sequence. The real answer comes from measuring, because it depends on the machine and the tool. Check the flop count in the report: synthesis tools may re-encode your FSM.

QuestionPractise Choosing a State Encoding →
Why can one-hot be fast, when the full-compare version above was not? Read this version. L1L2

Each arm tests one state bit, not the whole 5-bit vector. So "am I in RUN?" is one wire, and each next-state bit is a small OR of a few terms. On this flow it is 13 cells and 3 levels, against 27 and 5 for full-vector compares. The coding has to use the one-hot property for the encoding to save logic.

Follow-upPractise Why One-Hot Decoding Is Cheap →
What if the tool does not keep your encoding? How would you notice, and when does it matter? L2L3

Look at the flop count and the FSM section of the synthesis log. Here the binary machine came out with 5 flops: Yosys re-encoded it one-hot. That is often fine for speed. It matters when other logic reads the state code or the code crosses a clock domain. It also matters when recovery logic depends on it.

Follow-upPractise When the Synthesizer Re-Encodes Your FSM →
What if the state register is read in another clock domain, and the tool re-encodes it? L3

Then the reader may see a code that changes several bits at once, so it can sample a mix of two states. First, do not cross the state register at all. Cross a one-bit status through a synchronizer, or use a handshake. If you must keep the code, tell the tool not to re-encode it, and check the log after every run.

Follow-upPractise A State Code That Crosses a Clock Domain →
Compare a Gray-coded state register with one-hot. When would you pick Gray? L2

Pick Gray when the machine walks a fixed loop, like a counter, and you want one bit to change per step. Each step then toggles one flop instead of two. It also matters if the state is read in another clock domain, because a Gray step is never a torn value. One-hot is usually better for speed in a branching machine.

Follow-upPractise Gray-Coded States vs One-Hot →
Scale it up: a protocol controller has grown to 60 states and misses timing. What do you do? L3

First look for hidden counters: chains of states that only count cycles. Replace them with one counter and one waiting state. Then split the rest into a main machine and small sub-machines that talk through start and done signals. Each piece gets fewer states, smaller next-state logic, and its own tests.

Follow-upPractise Splitting a Very Large FSM →
The Sequencer Stuck After an Upset5
In a beam test, an upset hits a 3-bit state register. The sequencer then ignores start until reset. Here is the log. Explain it and fix it. L2L3

The enum names five codes, but three bits hold eight. The upset wrote 110, which has no arm. The block starts with st_d = st_q, so the machine holds 110 forever. Add a default arm that goes back to IDLE. Then check synthesis kept it, because a tool that re-encodes the FSM can drop arms for unreachable codes.

QuestionPractise The Sequencer Stuck After an Upset →
Why are there codes with no name at all? L1

The register is just N flops, so it can hold 2^N codes. The enum gives names to only the codes you use: five of eight here. The hardware does not know which codes have names. Only your next-state logic decides what happens in the other three.

Follow-upPractise Why a State Register Has Unnamed Codes →
Compare the two fixes: adding a default arm, and telling the tool to keep the encoding. Do you need both? L2L3

Yes, you need both. The default arm says what unnamed codes must do. Keeping the encoding makes sure the netlist still contains those codes and that arm. On this flow, the default alone was dropped by re-encoding, and the attribute alone kept a machine with no way out. Other tools offer a "safe FSM" option for the same job.

Follow-upPractise A Default Arm vs Keeping the Encoding →
Prove it: how do you show that the recovery survived synthesis? L2L3

Check the netlist, not the RTL. Simulate the gates, force each unnamed code into the state flops, and check the machine reaches IDLE within a few cycles. Read the FSM section of the synthesis log and the flop count too. They show whether the machine was extracted and re-encoded.

Follow-upPractise Proving the Netlist Still Recovers →
The block goes into a car. What do you require of every FSM, and how do you prove it? L3L4

Every machine must leave any illegal code in a bounded number of cycles, to a known safe state. It should also flag the event, so the system knows something went wrong. Prove the recovery on the netlist, not the RTL: check the encoding was kept and run upset tests at gate level. A formal check that every code reaches IDLE covers all codes at once.

Follow-upPractise FSM Recovery for a Safety-Critical Block →

Your Review Queue

Saved review reminders are coming soon.