Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Find the longest unique token run

Q101·Free·SystemVerilog

Find the longest unique token run

Difficulty
Medium
Topic
Streaming Algorithms
Language
SV
Interview prompt

Question

For each bounded protocol-token frame, report the longest contiguous run containing no repeated token ID, including its inclusive start and end indices. Supply 1 through 64 accepted tokens and assert in_last only on the final accepted token.

Candidate starting point

Implementation scaffold

module longest_unique_token_run (
  input  logic       clk,
  input  logic       rst_n,
  input  logic       in_valid,
  input  logic       in_last,
  input  logic [3:0] token,
  input  logic       result_ready,
  output logic       in_ready,
  output logic       result_valid,
  output logic [6:0] best_length,
  output logic [5:0] best_start,
  output logic [5:0] best_end
);
  typedef enum logic [1:0] {S_CLEAR, S_INPUT, S_RESULT} state_t;
  state_t state_q;

  logic last_valid_q [0:15];
  logic [5:0] last_index_q [0:15];
  logic [3:0] clear_q;
  logic [5:0] index_q, window_start_q;
  logic [6:0] best_length_q, result_length_q;
  logic [5:0] best_start_q, best_end_q;
  logic [5:0] result_start_q, result_end_q;

  logic [5:0] next_window_start;
  logic [6:0] current_length;
  logic current_is_better;

  always_comb begin : advance_unique_window
    // TODO: Implement advance_unique_window using the supplied state and interface.
  end

  assign in_ready      = (state_q == S_INPUT);
  assign result_valid  = (state_q == S_RESULT);
  assign best_length   = result_length_q;
  assign best_start    = result_start_q;
  assign best_end      = result_end_q;

  always_ff @(posedge clk) begin : clear_capture_and_publish
    // TODO: Implement clear_capture_and_publish using the supplied state and interface.
  end
endmodule
Reviewed example

Trace one case

Input
accepted token frame = [1, 2, 1, 3, 4, 3]
Expected output
best_start=1; best_end=4; best_length=4; run=[2, 1, 3, 4]

The repeated 1 advances the window start to index 1, and the later repeated 3 closes the longest unique run.

What to cover

Requirements

  1. Support token IDs 0 to 15 and frame lengths from 1 to 64.
  2. Track only accepted tokens; bubbles and downstream backpressure must not change indices.
  3. Never move the current window start backward when a repeated token lies before the active window.
  4. On equal maximum lengths, keep the run with the earliest start index.
  5. Clear or invalidate last-seen state between frames and hold the final result stable.
Exact question handoffPractice Q101

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

RTL Synthesis and Proof

Review widths, parameterization, memory inference, pipelining, implementation pressure, and proof evidence.

  • Streaming Algorithms
  • Sliding window
  • Last seen
  • Streaming
RTL Synthesis and Proof →
Continue practicing

Related questions

Q1153 · Reference ModelsModel a recent-ID replay streamHardP→Q973 · Reference ModelsDebug a signed sliding-window peakMediumP→
ASIC.FYI · Learn silicon end to end.info@asic.fyi