Home › VLSI Interview Questions › Constraints

10 SystemVerilog Constraint Interview Questions with Code

Constraint-writing questions are very common in DV interviews. Each answer shows the constraint code and the trap to avoid.

Updated 05 November 2026 · Written by Semicon Weekly in our own words · 10 questions

Practise these 10 questions free: type, speak or pick from 4 options and get an instant AI score.

Start practising →
mediumArrayscode

1.Write a constraint for a dynamic array of size 6 to 10 with unique even values between 16 and 127.

Show answer

arr.size() inside {[6:10]}; foreach (arr[i]) { arr[i] inside {[16:127]}; arr[i] % 2 == 0; } unique {arr}; (or nested foreach with i != j check if unique not allowed)

Common mistake: Forgetting to constrain size

Interviewer may ask next: Do it without the unique keyword.

Answer this in the AI interview bot →
mediumPatternscode

2.Write a constraint to generate unique array elements without using unique.

Show answer

foreach (a[i]) foreach (a[j]) if (i != j) a[i] != a[j];

Answer this in the AI interview bot →
mediumBitscode

3.Constrain a 32-bit variable so that exactly one bit is high (one-hot).

Show answer

$countones(x) == 1; or x != 0 && (x & (x - 1)) == 0

Interviewer may ask next: Now exactly 12 bits high, none adjacent.

Answer this in the AI interview bot →
mediumRealcode

5.How do you generate random real numbers between 1.35 and 2.68?

Show answer

rand real is not supported by many tools; randomize an int scaled value, e.g. rand int v; v inside {[135:268]}; then real r = v / 100.0 in post_randomize

Common mistake: Declaring rand real and assuming it works everywhere

Answer this in the AI interview bot →
mediumPatternscode

6.Write a constraint to fill an array with the pattern 0 1 0 2 0 3 0 4 ...

Show answer

foreach (a[i]) if (i % 2 == 0) a[i] == 0; else a[i] == (i + 1) / 2;

Answer this in the AI interview bot →
mediumPatternscode

7.Array of size 10: first five ascending, last five descending, all multiples of 5.

Show answer

foreach (a[i]) { a[i] % 5 == 0; if (i > 0 && i < 5) a[i] > a[i-1]; if (i > 5) a[i] < a[i-1]; }

Common mistake: Off-by-one in the index conditions

Answer this in the AI interview bot →
hardMathcode

8.Write a constraint that generates prime numbers between 1 and 100.

Show answer

Build a list of primes (2..97) in pre_randomize or a helper function, then constrain x inside {primes}; a function called inside a constraint is evaluated before solving, so it cannot search divisors of the value being solved

Answer this in the AI interview bot →
mediumOverridetheory

9.How can you override an existing constraint?

Show answer

Extend the class and redefine a constraint with the SAME name (replaces it); add a new constraint in the child (adds to it); inline randomize() with {} (adds); turn off with constraint_mode(0); conflicts make randomize() fail and return 0

Answer this in the AI interview bot →
mediumArrayscode

10.Write a constraint for int arr[2][4] so every element is even and less than 50.

Show answer

foreach (arr[i, j]) { arr[i][j] % 2 == 0; arr[i][j] < 50; arr[i][j] >= 0; }

Common mistake: Forgetting the lower bound for signed int (negatives appear)

Answer this in the AI interview bot →

Done reading? Take a 5-question mock interview and see your strong and weak topics.

Take a mock interview →

More interview questions