← RTL Design Interviews

Interview Question · Code Writing

A 1011 Sequence Detector with Overlap

L0 FresherL1 Juniorlogic puzzlesfsmsequence detectors
  • Asked at Apple
  • Asked at NVIDIA

The Question

The interviewer asks

Design an FSM that raises match for every 1011 on a serial input, including overlapping ones.

Reveal the Model Answer

Name each state by the longest prefix of 1011 seen so far: none, 1, 10 and 101. In 101, an input of 1 completes the pattern, so match goes high. The next state is 1, because that last 1 can start the next pattern. An input of 0 in 101 goes to 10, not to the start. This Mealy version needs four states.

The Overlapping 1011 DetectorEach arrow is a transition at a clock edge, labelled with the bit sampled there. Two self-loops are listed in the node text: S0 stays on 0 and S1 stays on 1.
The overlapping 1011 detectorEach arrow is a transition at a clock edge, labelled with the bit sampled there. Two self-loops are listed in the node text: S0 stays on 0 and S1 stays on 1. component S0 seen nothing; stayson 0 component S1 seen "1"; stays on 1 component S10 seen "10" component S101 seen "101"; a 1 nowcompletes 1011 10101: match0
  1. Climb the pattern

    Each expected bit moves the machine one state to the right: 1, then 0, then 1 reaches S101.

    Follow the calls, values and waits
    1. data sampled bit: S0 → S1
    2. data sampled bit: S1 → S10
    3. data sampled bit: S10 → S101
  2. A match keeps its last 1

    From S101 a 1 completes 1011 and raises match. The stream now ends in "11", and its longest tail that starts 1011 is "1", so the next state is S1.

    Follow the calls, values and waits
    1. data sampled bit: S101 → S1
  3. A miss keeps what it can

    From S101 a 0 makes "1010". Its tail "10" still starts 1011, so the next state is S10. From S10 a 0 makes "100", and no tail starts 1011: back to S0.

    Follow the calls, values and waits
    1. data sampled bit: S101 → S10
Read the diagram as text
  • S0: seen nothing; stays on 0
  • S1: seen "1"; stays on 1
  • S10: seen "10"
  • S101: seen "101"; a 1 now completes 1011
  • S0 → S1: 1
  • S1 → S10: 0
  • S10 → S101: 1
  • S10 → S0: 0
  • S101 → S1: 1: match
  • S101 → S10: 0

The restarting detector of the hook has both arrows out of S101 pointing to S0.

The Stream 1011011 Through Two DetectorsOne bit is sampled at each edge, from edge 2. The top pair restarts from S0 after a match; the bottom pair keeps the tail of the stream that can start the next match.
0123456clkdinst_restartS1S10S101S0S1match_restartst_overlapS1S10S101S1S10S101S1match_overlap
  1. Cycle 0

    Edge 2 samples a 1. Both detectors move from S0 to S1: they have seen "1", the first bit of 1011.

  2. Cycle 2

    Edge 4 samples the second 1. Both have seen "101" and sit in S101, one bit short of a match.

  3. Cycle 3

    Edge 5 samples a 1: both report a match. The restarting detector goes to S0. The other goes to S1, because the 1 it just took is also the first bit of 1011.

  4. Cycle 5

    Edge 7: the overlapping detector has seen "101" again. The restarting one has seen only "1" since its restart.

  5. Cycle 6

    Edge 8 completes the second 1011. Only the overlapping detector reports it: two matches against one.

Measured on the lesson's bench with a probe that also prints the state, edges 2 to 8. The match output is a register, so it shows the bit that the same edge sampled.

State (seen)din = 0din = 1
S0 (none)S0S1
S1 (1)S10S1
S10 (10)S0S101
S101 (101)S10S1, match = 1

Each arrow goes to the longest suffix of the bits so far that is still a prefix of 1011.

The stream 1011011011 holds three overlapping marks.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: match after bit 3 match after bit 6 match after bit 9 stream 1011011011: 3 matches PASS: three overlapping marks, each found

Self-Check

Tick each key point you covered in your answer.

What Interviewers Listen For (Fresher)

A good fresher answer draws the four states, each named by the bits seen so far. It traces a stream by hand before writing code.

Cross-Questions

Where the interviewer goes next. Open a node to practise that follow-up.

What IfProve ItBreak ItCompare

  1. A 1011 Sequence Detector with Overlap
  2. CompareMoore vs Mealy for the Same DetectorL0 FresherL1 Junior

    Compare a Moore version of the 1011 detector with the Mealy one. What changes?

    Reveal the Model Answer

    The Moore version needs a fifth state, "1011 seen", and match comes from that state alone. So it rises one cycle later, after the edge that enters the state. The Mealy output rises in the cycle of the last bit. But it can also glitch with the input.

    RUNIcarus Verilog · in your browser
    Edit the code, then run it
    Expected: cycle 3 din=1 mealy=1 moore=0 cycle 4 din=0 mealy=0 moore=1

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "Moore and Mealy detectors need the same number of states."

    The Moore output needs its own state for "pattern seen", so this detector needs five states, not four.

    Source Sutherland, RTL Modeling with SystemVerilog (2017) ch. 8.2.1 Mealy and Moore FSM architectures

  3. Break ItThe Detector That Missed 1011 Inside 11011L1 Junior

    A detector passes the stream 1011 but misses the mark in 11011. Which transition is wrong?

    systemverilog
    S1:   if (din) nxt = S0; else nxt = S10;   // in state "1", another 1 arrives
    Reveal the Model Answer

    In state 1, another 1 still leaves a 1 as the newest bit. So the machine must stay in S1. Going to S0 throws away a valid start, and 11011 never reaches S101.

    Self-Check

    Tick each key point you covered in your answer.

    Missed Corner

    "If 1011 passes, every stream passes."

    One stream exercises only some transitions. The twin shows 11011 failing because S1 on 1 is never taken by 1011.

    Run It

    The detector with S1 on 1 going to S0.

    RUNIcarus Verilog · in your browser
    Edit the code, then run it
    Expected: stream 11011: 0 matches FAIL: a 1 after a 1 must keep the S1 state; restarting misses 1011 inside 11011
    See the Fix
    module det1011 (input logic clk, rst_n, din, output logic match);
      typedef enum logic [1:0] {S0, S1, S10, S101} state_t;   // the longest prefix of 1011 seen so far
      state_t state, nxt;
      always_ff @(posedge clk or negedge rst_n)
        if (!rst_n) state <= S0; else state <= nxt;
      always_comb begin
        nxt = S0; match = 0;
        case (state)
          S0:   if (din) nxt = S1;   else nxt = S0;
          S1:   if (din) nxt = S1;   else nxt = S10;
          S10:  if (din) nxt = S101; else nxt = S0;
          S101: begin
                  match = din;
                  if (din) nxt = S1; else nxt = S10;   // the last 1 can start the next 1011
                end
        endcase
      end
    endmodule
    module tb;
      logic clk = 0, rst_n = 0, din = 0, match;
      logic [0:4] stream = 5'b11011;
      int hits = 0;
      det1011 d (.*);
      always #5 clk = ~clk;
      initial begin
        @(negedge clk) rst_n = 1;
        for (int i = 0; i < 5; i++) begin
          din = stream[i];
          #1 if (match) begin hits++; $display("match after bit %0d", i); end
          @(negedge clk);
        end
        $display("stream 11011: %0d matches", hits);
        if (hits == 1) $display("PASS: 1011 found inside 11011");
        else $display("FAIL: expected 1 match");
        $finish;
      end
    endmodule
    

    Prints: PASS: 1011 found inside 11011

  4. Prove ItSolve R3.4.1: Frame Marks That OverlapSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem R3.4.1

  5. What IfA Shift Register Instead of an FSML1 Junior

    Asked if they ask whether an FSM is needed at all

    What if you skip the FSM and keep the last bits in a shift register?

    Reveal the Model Answer

    Keep the last three bits and compare them, with the current bit, against 1011. Overlap needs no extra logic, because every window is checked. It costs one flop per pattern bit, while the FSM needs only enough flops to count its states. For long patterns, the FSM is smaller.

    RUNIcarus Verilog · in your browser
    Edit the code, then run it
    Expected: shift register: 3 matches in 1011011011

    Self-Check

    Tick each key point you covered in your answer.

    Half-Truth

    "The shift register is always the cheaper choice."

    It needs a flop for every pattern bit minus one. A 16-bit pattern needs 15 flops, but an FSM with 16 states needs only 4 in binary.

  6. What IfThe Smallest Sequence Detector: A Rising EdgeL0 Fresher

    What if the pattern is only two bits long? Detect a rising edge on a signal already in the clock domain.

    Reveal the Model Answer

    Register the signal once and compare old with new: rise = sig & ~sig_q. It is a detector for the sequence 01. The pulse lasts one clock. If sig comes from another clock domain, synchronize it first.

    RUNIcarus Verilog · in your browser
    Edit the code, then run it
    Expected: pattern 0110101100: 3 rising edges counted PASS: one pulse per rising edge

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    Writes rise = sig & ~sig with no flop.

    A signal ANDed with its own inverse is 0 in logic. An edge needs the value from the previous cycle, which only a flop holds.

    Run It

    The edge detector without the flop.

    RUNIcarus Verilog · in your browser
    Edit the code, then run it
    Expected: pattern 0110101100: 0 rising edges counted FAIL: sig & ~sig is always 0; an edge needs the old value from a flop
    See the Fix
    module rise_det (input logic clk, sig, output logic rise);
      logic sig_q = 0;
      always_ff @(posedge clk) sig_q <= sig;   // sig one cycle ago
      assign rise = sig & ~sig_q;   // high now, low last cycle
    endmodule
    module tb;
      logic clk = 0, sig = 0, rise;
      logic [0:9] pattern = 10'b0110101100;   // three rising edges
      int n = 0;
      rise_det r (.*);
      always #5 clk = ~clk;
      initial begin
        for (int i = 0; i < 10; i++) begin
          sig = pattern[i];
          #1 if (rise) n++;
          @(negedge clk);
        end
        $display("pattern 0110101100: %0d rising edges counted", n);
        if (n == 3) $display("PASS: one pulse per rising edge");
        $finish;
      end
    endmodule
    

    Prints: PASS: one pulse per rising edge

  7. Prove ItSolve R2.5.1: One Press, One PulseSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem R2.5.1

  8. Prove ItSolve R3.4.1: Frame Marks That OverlapSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem R3.4.1

What People Get Wrong

Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.

Misconception

Goes back to the start state after every match.

That is a non-overlapping detector. The final 1 of one mark is the first bit of the next, so marks get lost.

Run It

The detector that returns to S0 after a match, on the same stream.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: stream 1011011011: 2 matches FAIL: going back to S0 after a match misses the overlapping marks
See the Fix
module det1011 (input logic clk, rst_n, din, output logic match);
  typedef enum logic [1:0] {S0, S1, S10, S101} state_t;   // the longest prefix of 1011 seen so far
  state_t state, nxt;
  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n) state <= S0; else state <= nxt;
  always_comb begin
    nxt = S0; match = 0;
    case (state)
      S0:   if (din) nxt = S1;   else nxt = S0;
      S1:   if (din) nxt = S1;   else nxt = S10;
      S10:  if (din) nxt = S101; else nxt = S0;
      S101: begin
              match = din;
              if (din) nxt = S1; else nxt = S10;   // the last 1 can start the next 1011
            end
    endcase
  end
endmodule
module tb;
  logic clk = 0, rst_n = 0, din = 0, match;
  logic [0:9] stream = 10'b1011011011;
  int hits = 0;
  det1011 d (.*);
  always #5 clk = ~clk;
  initial begin
    @(negedge clk) rst_n = 1;
    for (int i = 0; i < 10; i++) begin
      din = stream[i];
      #1 if (match) begin hits++; $display("match after bit %0d", i); end
      @(negedge clk);
    end
    $display("stream 1011011011: %0d matches", hits);
    if (hits == 3) $display("PASS: three overlapping marks, each found");
    else $display("FAIL: expected 3 matches");
    $finish;
  end
endmodule

Prints: PASS: three overlapping marks, each found

Missed Corner

Sends every mismatch back to the start state.

A mismatch can still leave a useful suffix. In 101 a 0 leaves 10, and in 1 another 1 leaves 1.

Source Sutherland, RTL Modeling with SystemVerilog (2017) ch. 8.2 Modeling Finite State Machines (FSMs)

Delivery

Starts coding before asking whether overlaps count.

The spec changes the answer. A good candidate asks about overlap and about Mealy or Moore output first.

Learn It, Then Prove It