Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • Start Here6
  • SystemVerilog Language Foundations28
  • Object-Oriented SystemVerilog for Verification30
  • Constraint Randomization — Interview-Critical Deep Dive36
    • Randomization Basics: rand, randc & randomize()
    • Constraint Operators & Expressions
    • Dynamic Constraint Control
    • Randomizing Arrays and Queues
    • How the Constraint Solver Works
    • Debugging Randomization Failures
    • Interview Drills — Constraints and Randomization Q&A
      • Even/Odd, Alignment & Divisibility
      • Powers of 2, One-Hot & Bit Games
      • Unique Values & Ordering
      • Sum, Checksum & Distribution Drills
      • Dependent-Field Puzzles
      • Q&A: rand, randc & randomize()
      • Q&A: dist, inside, soft, solve-before
      • Q&A: Controlling Randomization
      • Q&A: Arrays & Nested Objects
  • SystemVerilog Assertions — Interview-Critical Deep Dive40
  • Functional Coverage in SystemVerilog22
  • Testbench Architecture in SystemVerilog23
  • Advanced Language & Integration25
  • Senior & Interview Preparation21
  • Principal-Level Verification28
Loading lesson

‹Interview Drills — Constraints and Randomization Q&A
  1. Topics
  2. SystemVerilog
  3. Constraint Randomization — Interview-Critical Deep Dive
  4. Interview Drills — Constraints and Randomization Q&A
  5. Unique Values & Ordering

Part 3 · Constraint Randomization · Intermediate

Unique Values & Ordering

Drill: all different values, a strictly ascending array, and a unique maximum.

On this page

    Why this matters

    A tag generator used unique and still produced duplicate tags. The tool had ignored the keyword, and nothing in the bench counted repeats.

    After this lesson you can: make values distinct in a way every tool honours, order an array with neighbour rules, and place a unique maximum with a helper index.

    The three prompts

    Drill 1. rand bit [3:0] v[8]: all eight values are different.

    Drill 2. rand bit [7:0] a[]: 5 to 8 values, strictly ascending.

    Drill 3. rand bit [7:0] a[6]: exactly one element is bigger than all the others. A helper field rand bit [2:0] top is there if you want it.

    Write the three answers before opening the reveal. Copy the starter into the Code Lab, fill in each empty answer_c, and run it on Verilator. The grader solves each class hundreds of times and checks every result, then pins a few values that must be legal; it prints ok or the first rule your answer broke.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    // Drill starter: write each answer_c, then run it.
    // Drills: unique values and ordering.
    class drill1;                                   // 8 different values, each 0 to 15
      rand bit [3:0] v[8];
      constraint answer_c { /* your answer here */ }
    endclass
    
    class drill2;                                   // 5 to 8 values, strictly ascending
      rand bit [7:0] a[];
      constraint answer_c { /* your answer here */ }
    endclass
    
    class drill3;                                   // exactly one element is bigger than all the others
      rand bit [7:0] a[6];
      rand bit [2:0] top;                           // a helper index: use it or not
      constraint answer_c { /* your answer here */ }
    endclass
    
    Predict first Before you write drill 1: an interviewer suggests randc. Does it help?

    No. randc stops a value repeating across successive calls of one field. It says nothing about eight elements within one call, and it cannot be combined with rand on the same field.

    Check yourself Reveal one set of answers (open only after your own run)

    One set of answers, run through the same grader:

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // Drills: unique values and ordering.
    class drill1;                                   // 8 different values, each 0 to 15
      rand bit [3:0] v[8];
      constraint answer_c { foreach (v[i]) foreach (v[j]) if (i < j) v[i] != v[j]; }
    endclass
    
    class drill2;                                   // 5 to 8 values, strictly ascending
      rand bit [7:0] a[];
      constraint answer_c { a.size() inside {[5:8]}; foreach (a[i]) if (i > 0) a[i] > a[i-1]; }
    endclass
    
    class drill3;                                   // exactly one element is bigger than all the others
      rand bit [7:0] a[6];
      rand bit [2:0] top;                           // a helper index: use it or not
      constraint answer_c { top < 6; foreach (a[i]) if (i != top) a[i] < a[top]; }
    endclass
    
    Terminal
    drill 1, all different: ok
    drill 2, strictly ascending: ok
    drill 3, unique maximum: ok
    PASS: all 3 drills right

    Why these answers

    Answer

    Spelling

    Verdict

    unique

    unique { v }

    The standard's one keyword; see the tool note below

    Pairwise

    if (i < j) v[i] != v[j]

    Portable: 28 relations for 8 elements

    randc

    Cycling across calls

    Wrong tool: says nothing within one solve

    The reveal uses the pairwise form on purpose. Verilator 5.048 ignored unique { v } here: with no other rule naming the elements, 85 of 100 results repeated a value. With a range rule beside it, it worked. The pairwise form works on every tool, so it is the safe interview answer, and saying why is a strong follow-up.

    Ordering needs only neighbour relations, because they chain: a[0] < a[1] < ... < a[n-1]. Strict ascent therefore gives uniqueness for free. The low-side guard if (i > 0) keeps the loop inside the array.

    The unique maximum takes a random index for the position, and every other element stays strictly below a[top]. Pinning the maximum to a fixed slot is right only when the spec fixes the slot, and the grader pins maxima at index 0 and index 5 to catch it.

    Common pitfall

    Neighbour inequality is not uniqueness: a[0] may still equal a[2]. Uniqueness is pairwise, ordering is between neighbours, and mixing the two is the standard whiteboard fault.

    Interview

    Declaring rand and randc together on one field is illegal SystemVerilog, not a tool quirk. Say so, and offer the pairwise rule or a shuffled list instead.

    Remember

    • Uniqueness is pairwise; ordering is between neighbours.

    • Strict ascent gives uniqueness for free.

    • randc works across calls, never within one array.

    Rule of thumb: a distinctness rule is trusted only after a grader counts repeats.

    How this was checked

    Run with Verilator 5.048 (--binary --timing) on 2026-10-18; every bench printed the PASS line shown. Verilator prints a harmless WIDTHTRUNC warning for if (!obj.randomize()), because randomize() returns an int, and an UNSATCONSTR warning when a solve fails on purpose. Icarus 13 does not support constraints.

    Each starter was run with its answers left empty, and its grader reported all three drills wrong; each revealed answer passed the same grader. The graders also caught an unwidened size * count and an over-tight even rule when tried.

    unique { v } on a fixed array with no other element rule repeated a value in 85 of 100 results; with a range rule beside it, none did.

    Related topics

    • Sum, Checksum and Distribution Drills

    Previous

    94 / 259

    Next: Sum, Checksum & Distribution Drills

    Up next

    Up nextSum, Checksum & Distribution Drills

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Even/Odd, Alignment & Divisibility
    • Dependent-Field Puzzles
    • Q&A: rand, randc & randomize()