Interview Question · Code Writing
A 1011 Sequence Detector with Overlap
- 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.
You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
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.
- 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
- data sampled bit: S0 → S1
- data sampled bit: S1 → S10
- data sampled bit: S10 → S101
- 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
- data sampled bit: S101 → S1
- 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
- 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.
- Cycle 0
Edge 2 samples a 1. Both detectors move from S0 to S1: they have seen "1", the first bit of 1011.
- Cycle 2
Edge 4 samples the second 1. Both have seen "101" and sit in S101, one bit short of a match.
- 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.
- Cycle 5
Edge 7: the overlapping detector has seen "101" again. The restarting one has seen only "1" since its restart.
- 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 = 0 | din = 1 |
|---|---|---|
| S0 (none) | S0 | S1 |
| S1 (1) | S10 | S1 |
| S10 (10) | S0 | S101 |
| S101 (101) | S10 | S1, 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.
match after bit 3 match after bit 6 match after bit 9 stream 1011011011: 3 matches PASS: three overlapping marks, each foundSelf-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.
What Interviewers Listen For (Junior)
A good junior answer explains each back edge: keep the longest recent tail that still starts the pattern. It says whether match is Mealy or Moore, and what that costs.
Cross-Questions
Where the interviewer goes next. Open a node to practise that follow-up.
What IfProve ItBreak ItCompare
- A 1011 Sequence Detector with Overlap
-
CompareMoore vs Mealy for the Same DetectorL0 FresherL1 Junior
Compare a Moore version of the
1011detector with the Mealy one. What changes?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
The Moore version needs a fifth state, "1011 seen", and
matchcomes 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 browserExpected:cycle 3 din=1 mealy=1 moore=0cycle 4 din=0 mealy=0 moore=1Self-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
-
Break ItThe Detector That Missed 1011 Inside 11011L1 Junior
A detector passes the stream
1011but misses the mark in11011. Which transition is wrong?systemverilog S1: if (din) nxt = S0; else nxt = S10; // in state "1", another 1 arrives2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
In state
1, another 1 still leaves a1as the newest bit. So the machine must stay inS1. Going toS0throws away a valid start, and11011never reachesS101.Self-Check
Tick each key point you covered in your answer.
Missed Corner
"If
1011passes, every stream passes."One stream exercises only some transitions. The twin shows
11011failing becauseS1on 1 is never taken by1011.Run It
The detector with S1 on 1 going to S0.
RUNIcarus Verilog · in your browserExpected:stream 11011: 0 matchesFAIL: a 1 after a 1 must keep the S1 state; restarting misses 1011 inside 11011See 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 endmodulePrints:
PASS: 1011 found inside 11011 -
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.
-
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?
2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
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 browserExpected:shift register: 3 matches in 1011011011Self-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.
-
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.
2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
Register the signal once and compare old with new:
rise = sig & ~sig_q. It is a detector for the sequence01. The pulse lasts one clock. Ifsigcomes from another clock domain, synchronize it first.RUNIcarus Verilog · in your browserExpected:pattern 0110101100: 3 rising edges countedPASS: one pulse per rising edgeSelf-Check
Tick each key point you covered in your answer.
Misconception
Writes
rise = sig & ~sigwith 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 browserExpected:pattern 0110101100: 0 rising edges countedFAIL: sig & ~sig is always 0; an edge needs the old value from a flopSee 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 endmodulePrints:
PASS: one pulse per rising edge -
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.
-
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.
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.
stream 1011011011: 2 matches FAIL: going back to S0 after a match misses the overlapping marksSee 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.