TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

FSM Interview Questions: Sequence Detection and Divide by 3

The Editor's Desk
by
0
Share Facebook X LinkedIn WhatsApp Email

These three finite state machine (FSM) questions are classic interview material for digital design and RTL roles. Each one tests a different skill: counting with states, doing arithmetic one bit at a time, and reasoning about how many flip-flops a combined machine needs. Try them first, then read the worked answers below.

  • Design an FSM that has 1 i/p and 1 o/p. The o/p becomes 1 and remains 1 when at least two 0's and two 1's have occurred as i/p's.
  • Design a "%3" FSM that accepts one bit at a time, most significant bit first, and indicates if the number is divisible by 3.
  • If an FSM is redesigned using a state register with minimum number of bits after connecting the output of a 3-state FSM to the inputs of an 9-state FSM, what is the maximum number of bits needed?

What an FSM is, in plain words

A finite state machine is a circuit that remembers a small amount of history in a state register and decides what to do next from that state and the current input. On every clock edge it moves to a next state. Its output depends either on the state alone (a Moore machine) or on the state and the current input (a Mealy machine).

The design recipe is always the same:

  1. Decide what the machine must remember. Each distinct "thing to remember" becomes a state.
  2. Draw the state diagram: one circle per state, one arrow per input value.
  3. Choose a state encoding (binary, one-hot or Gray) and write the next-state and output logic.
  4. Check the reset state and any unused encodings.

The hard part is step 1. Interviewers are mostly watching whether you find the smallest set of facts the machine needs to remember.

Question 1 in outline: counting zeros and ones

The machine only needs to know how many 0s it has seen, capped at two, and how many 1s it has seen, capped at two. Once both counts reach two the output goes high and stays high. That gives a three-by-three grid of states.

Question 2 in outline: divisible by 3

You never need to store the whole number, only its remainder when divided by 3. There are three possible remainders, so three states. Each new bit doubles the number so far and adds the bit.

Question 3 in outline: how many state bits

When two machines are combined into one, the combined state is the pair of their states. In the worst case every pair can occur, so the count of states multiplies, and the number of bits is the base-2 logarithm of that product, rounded up.

Deep dive: the numbers and details

This part holds the full state tables, encodings, Verilog and the arithmetic. Skip it if the overview is all you need.

Question 1: at least two 0s and two 1s

Name each state ZaOb, where a is the number of 0s seen (0, 1 or 2+) and b the number of 1s seen (0, 1 or 2+). Reset is Z0O0. An input 0 increments a (saturating at 2); an input 1 increments b (saturating at 2). The output is 1 only in Z2O2, which is absorbing: every input keeps the machine there, so the output "remains 1".

StateNext on 0Next on 1Output
Z0O0Z1O0Z0O10
Z0O1Z1O1Z0O20
Z0O2Z1O2Z0O20
Z1O0Z2O0Z1O10
Z1O1Z2O1Z1O20
Z1O2Z2O2Z1O20
Z2O0Z2O0Z2O10
Z2O1Z2O1Z2O20
Z2O2Z2O2Z2O21

Nine states need ceil(log2 9) = 4 flip-flops in binary encoding. A neater implementation uses two independent 2-bit saturating counters, one for zeros and one for ones, which is the same nine-state machine factored into two parts, and the output is simply (zeros == 2) AND (ones == 2). This is a Moore machine. A Mealy version can assert the output one cycle earlier, on the input that completes the condition.

Check with the input sequence 1, 1, 1, 0, 0: the states visited are Z0O1, Z0O2, Z0O2, Z1O2, Z2O2, so the output rises after the fifth bit and stays high.

module two_and_two (
  input  wire clk, rst, din,
  output wire dout
);
  reg [1:0] zeros, ones;   // saturating counters, 0..2
  always @(posedge clk)
    if (rst) begin
      zeros <= 2'd0;
      ones  <= 2'd0;
    end else if (din) begin
      if (ones  != 2'd2) ones  <= ones  + 2'd1;
    end else begin
      if (zeros != 2'd2) zeros <= zeros + 2'd1;
    end
  assign dout = (zeros == 2'd2) && (ones == 2'd2);
endmodule

Question 2: divisible by 3, MSB first

If the value so far is N and a new bit b arrives, the new value is 2N + b. Only the remainder matters, so with r = N mod 3:

r_next = (2 * r + b) mod 3
Remainder stateNext on 0Next on 1Output (divisible)
R0R0R11
R1R2R00
R2R1R20

Worked example, input 1, 1, 0, 0 (decimal 12): R0 to R1 (value 1), R1 to R0 (value 3), R0 to R0 (value 6), R0 to R0 (value 12). The machine ends in R0, so 12 is divisible by 3. Input 1, 0, 1, 1 (decimal 11): R1, R2, R2, R2, so 11 leaves remainder 2.

Three states need two flip-flops. The same idea works for any modulus m: m states, with r_next = (2r + b) mod m. If the bits arrive least significant bit first instead, the machine must also track the current power of two mod 3, which alternates 1, 2, 1, 2, so it needs six states (or three states plus one parity bit).

module div3 (
  input  wire clk, rst, din,
  output wire div_by_3
);
  reg [1:0] r;   // remainder 0, 1 or 2
  always @(posedge clk)
    if (rst) r <= 2'd0;
    else case (r)
      2'd0:    r <= din ? 2'd1 : 2'd0;
      2'd1:    r <= din ? 2'd0 : 2'd2;
      2'd2:    r <= din ? 2'd2 : 2'd1;
      default: r <= 2'd0;   // unused code 3 recovers to reset
    endcase
  assign div_by_3 = (r == 2'd0);
endmodule

Question 3: state bits for two connected machines

The 3-state machine needs ceil(log2 3) = 2 bits and the 9-state machine needs ceil(log2 9) = 4 bits, so as two separate machines they use 6 flip-flops. Redesigned as one machine, the state is the pair (state of A, state of B). In the worst case all 3 x 9 = 27 pairs are reachable, and

ceil(log2 27) = 5 bits   (2^4 = 16 < 27 <= 32 = 2^5)

So the maximum is 5 bits. Merging saves one flip-flop here because 27 fits in 32 codes. If some pairs are unreachable (the output of A may constrain which states B can be in), the product machine can be minimized further and may need fewer bits, which is why the question asks for the maximum.

Encoding trade-offs

EncodingFlip-flops for 9 statesTypical use
Binary4Smallest register; more next-state logic
Gray4Sequential state changes flip one bit, lower switching
One-hot9Simple, fast next-state logic; common in FPGAs, which have many flip-flops

Common mistakes

  • Storing the full input history or a full binary number instead of the minimum facts (counts or a remainder).
  • Forgetting the "remains 1" requirement in question 1, so a later input drops the output.
  • Answering 6 bits for question 3 by adding the two machines' bits instead of taking the log of the product of their states.
  • Leaving unused encodings without a recovery path to reset.

Comments with alternative solutions are welcome.

The Editor's Desk
About the Author
The Editor's Desk
Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!