VLSI DV Interview Puzzles · All levels

if-else with Overlap on One Value

Compute P(val==2) and P(mode==1) by counting legal tuples. Why is val not uniform over {1,2,3}?

Puzzle

Difficulty: Medium · Puzzle 3 of 6 · Topic: Constraint Solving Order Puzzles

Compute P(val==2) and P(mode==1) by counting legal tuples. Why is val not uniform over {1,2,3}?

Code

systemverilog
class overlap_if;
  rand bit mode;
  rand int unsigned val;
  constraint c {
    val inside {[1:3]};
    if (mode) val inside {1,2};
    else      val inside {2,3};
  }
endclass

Hint

List legal (mode,val) pairs explicitly.

Step-by-step solution

diagram
1) mode=1 allows val {1,2} -> 2 tuples.
2) mode=0 allows val {2,3} -> 2 tuples.
3) Total legal tuples=4: (1,1),(1,2),(0,2),(0,3).
4) val==2 appears in two tuples -> P(val==2)=2/4=1/2.
5) mode==1 appears in two tuples -> P(mode==1)=1/2.

Answer

Answer: P(val==2)=1/2 and P(mode==1)=1/2; overlap on value 2 creates bias versus a naive 1/3-per-value assumption.

Why candidates get it wrong

People reason on variable domains separately and miss tuple multiplicity across condition branches.

Interviewer follow-up

What small constraint would make val exactly uniform over 1,2,3?

Related topics