← Design Verification Interviews

Interview Question · Concept

Why Writes Became Rare

L1 JuniorL2 Mid-Levelsystemverilograndomizationdistribution
  • Asked at Google

The Question

The interviewer asks

With this class, about how many of 900 bursts are writes? Why? How would you get roughly half?

systemverilog
class burst;
  rand bit       is_write;
  rand bit [2:0] beats;
  constraint c { is_write -> beats == 0; }  // writes are single-beat
endclass
Reveal the Model Answer

About 100, one in nine. The solver picks uniformly among all legal combinations. A read pairs with any of eight beats values, but a write pairs only with 0. So 1 of the 9 legal pairs is a write. solve is_write before beats chooses is_write first with equal odds and gives about half, without changing which pairs are legal.

The Same Nine Pairs, Picked Two WaysHow the solver chooses among the legal pairs of is_write and beats, with no order and with is_write first. Arrows show the choice moving from one stage to the next.
The same nine pairs, picked two waysHow the solver chooses among the legal pairs of `is_write` and `beats`, with no order and with `is_write` first. Arrows show the choice moving from one stage to the next. legal set Nine legal pairs a write with 1 beat;reads with 1 to 8beats choice No order every pair equallylikely result Writes: 1 in 9 about 11% of bursts choice is_write first solve is_write beforebeats choice Then beats among the values thatfit result Writes: 1 in 2 about 50% of bursts one pairis_writebeats
  1. Count the pairs

    The rules allow nine pairs: one write with 1 beat, and eight reads with 1 to 8 beats. Writes own one pair and reads own eight.

  2. No order

    With no order, the solver picks one of the nine pairs, each with the same chance. A write comes up once in nine picks.

    Follow the calls, values and waits
    1. data 9 pairs: Nine legal pairs → No order
    2. data write, 1 in 9: No order → Writes: 1 in 9
  3. Choose is_write first

    solve is_write before beats picks is_write first, 1 or 0 with equal chance, as if beats did not exist yet. Both values have at least one legal partner.

    Follow the calls, values and waits
    1. data 9 pairs: Nine legal pairs → is_write first
    2. data is_write = 1, 1 in 2: is_write first → Then beats
  4. Then beats

    A write leaves only 1 beat, so writes are half of the traffic. Each read length gets 1/2 × 1/8 = 1/16, and the legal pairs are still the same nine.

    Follow the calls, values and waits
    1. data beats = 1: Then beats → Writes: 1 in 2
Read the diagram as text
  • Nine legal pairs: a write with 1 beat; reads with 1 to 8 beats
  • No order: every pair equally likely
  • Writes: 1 in 9: about 11% of bursts
  • is_write first: solve is_write before beats
  • Then beats: among the values that fit
  • Writes: 1 in 2: about 50% of bursts
  • Nine legal pairs → No order
  • No order → Writes: 1 in 9: one pair
  • Nine legal pairs → is_write first
  • is_write first → Then beats: is_write
  • Then beats → Writes: 1 in 2: beats

An order changes probabilities only. It never makes a failing call succeed.

Self-Check

Tick each key point you covered in your answer.

What Interviewers Listen For (Junior)

A good junior answer counts the legal pairs out loud, eight reads and one write, before giving the ratio.

Cross-Questions

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

WhyWhat IfProve ItBreak ItCompare

  1. Why Writes Became Rare
  2. WhyWhy the Solver Weights Solutions, Not FieldsL1 JuniorL2 Mid-Level

    Why does the standard define the distribution over whole solutions rather than giving each random field its own fair odds?

    Reveal the Model Answer

    Constraints tie fields together, so there is no single "fair" order in which to pick them. Making every legal combination equally likely gives one clear answer. It does not depend on how the constraints are written or how the solver works inside. The price is that a constraint touching one field can skew another, and solve...before and dist exist to correct that.

    One Write in Nine Bursts
    012345678is_writebeats528137462the only write
    1. Cycle 3

      The only write so far. Reads have eight legal lengths and writes have one, so a write wins about one pick in nine.

    2. Cycle 8

      Nine bursts, one write. Every burst is legal and the constraints are correct: the mix comes from counting combinations.

    One column per generated burst. Every burst is legal; the mix is the problem.

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    "The solver picks fields in declaration order."

    Declaration order has no effect on the distribution; only solve...before imposes an order.

    Source IEEE 1800-2017 §18.5.10

  3. CompareDist Weights: Colon-Equals vs Colon-SlashL1 JuniorL2 Mid-Level

    Compare x dist {0 := 1, [1:3] := 1} with x dist {0 := 1, [1:3] :/ 1}. How often is x zero in each?

    Reveal the Model Answer

    := gives every value in the range the full weight, so 1, 2 and 3 weigh 1 each and 0 comes up about 1 time in 4. :/ splits the weight across the range, so 1, 2 and 3 share a weight of 1 and 0 comes up about half the time. A dist on is_write is another way to correct the bias, because it sets that field's odds directly.

    RUNVerilator · simulator service
    Edit the code, then run it
    Expected: := gives 0 about 1 time in 4 :/ gives 0 about 1 time in 2

    Self-Check

    Tick each key point you covered in your answer.

    Misconception

    ":= and :/ are the same for a range."

    They differ exactly for ranges: one weight per value against one weight per range. The run shows a quarter against a half.

    Source IEEE 1800-2017 §18.5.4

  4. Break ItA Dist That Lists a Forbidden ValueL2 Mid-Level

    What if a dist lists a value that another constraint forbids, say x dist {0 := 5, [1:3] := 1} together with x != 0?

    Reveal the Model Answer

    By the standard the forbidden value simply drops out, and the weights apply among the legal values. Verilator 5.052 instead fails some of those calls, so the bug shows up as randomization failures on one tool only. Keep every value in a dist legal under the other constraints, or move the weighting into a separate field.

    Self-Check

    Tick each key point you covered in your answer.

    Tool as Truth

    "Our simulator fails the call, so the constraint set is illegal."

    The standard says the weighted value is just unavailable. The failures are Verilator 5.052 behaviour, measured in lesson 5.2.

    Source Lesson 5.2 simulator note (12 of 20 calls failed on Verilator 5.052)

  5. What IfWhat Solve Before Cannot DoL2 Mid-Level

    Asked if they suggest solve...before

    What are the limits of solve...before?

    Reveal the Model Answer

    It changes only probabilities: the legal set, and therefore any conflict, is unchanged. The variables it orders must be rand; randc variables are not allowed, since they are already solved first. And the result is only as good as your tool. So you still measure the bias with coverage.

    Self-Check

    Tick each key point you covered in your answer.

    Spec Misquote

    "Put a randc field in solve...before to make it go first."

    The standard does not allow randc variables there; they are solved before other random variables anyway.

    Source IEEE 1800-2017 §18.5.10

  6. Prove ItSolve 5.3.2: Balance Mode FrequencySimulator-graded

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

    Open Problem 5.3.2

What People Get Wrong

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

Misconception

"is_write is a fair coin; the implication only filters the bad pairs."

Filtering keeps 9 pairs, and the solver weights pairs, not fields. The twin counts writes far below half; choosing is_write first restores it.

Run It

900 calls on the class above, counting writes.

RUNVerilator · simulator service
Edit the code, then run it
Expected: FAIL: only about 1 in 9 calls is a write, because 1 of the 9 legal pairs is a write
See the Fix
class burst;
  rand bit       is_write;
  rand bit [2:0] beats;
  constraint c { is_write -> beats == 0; }
endclass
module tb;
  burst t = new;
  int writes = 0;
  initial begin
    repeat (900) begin
      bit w;
      void'(std::randomize(w));                       // the fix: choose the kind first, 50/50
      void'(t.randomize() with { is_write == w; });
      writes += t.is_write;
    end
    $display("writes in 900 calls: %0d", writes);
    if (writes > 350 && writes < 550) $display("PASS: choosing is_write first gives about half writes");
  end
endmodule

Prints: PASS: choosing is_write first gives about half writes

Tool as Truth

"I added solve is_write before beats and still saw one write in nine, so it does nothing."

The standard defines its effect. Verilator 5.052 does not implement those probabilities, so a measurement there says nothing about the language.

Source Lesson 5.3 simulator note (Verilator 5.052 does not give solve...before its probabilities)

Misconception

"solve...before can make an unsolvable set of constraints solvable."

It only reorders the choice. The legal set is unchanged, so a conflict stays a conflict.

Source IEEE 1800-2017 §18.5.10

Delivery

Jumps to "add solve-before" without working out why the ratio is skewed.

The interviewer wants the counting argument first; the fix only makes sense once the 1-in-9 is explained.

Learn It, Then Prove It