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

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
    • Constraint Solving Order Puzzles
    • Distribution Puzzles (`dist`, `:=` vs `:/`)
    • solve...before Bias Puzzles
    • randomize() with Puzzles
      • Inline Implication That Looks Like a Conflict
      • Constraint Override by Name in a Derived Class
      • unique and Array Element Counting
      • constraint_mode Changes Pass/Fail
      • rand_mode(0) Freezes Value and Can Overconstrain
      • pre_randomize Assignment vs Solver and post_randomize Call Count
    • Randomization & Constraint Puzzles: Tricky Q&A
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←randomize() with Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. Randomization & Constraint Puzzles
  4. randomize() with Puzzles
  5. unique and Array Element Counting
On this page

    VLSI DV Interview PuzzlesAll levels

    unique and Array Element Counting

    How many solutions does the class have, and what is P(a[0]==2)? Then: how many solutions if the foreach bound becomes a[i] < 3, and how many if an ascending-order constraint a[0]<a[1]<a[2]<a[3] is added to the original?

    Puzzle

    Difficulty: Medium · Puzzle 3 of 6 · Topic: randomize() with Puzzles

    How many solutions does the class have, and what is P(a[0]==2)? Then: how many solutions if the foreach bound becomes a[i] < 3, and how many if an ascending-order constraint a[0]<a[1]<a[2]<a[3] is added to the original?

    Code

    design.sv
    1
    2
    3
    4
    5
    class uq;
      rand bit [3:0] a[4];
      constraint c_rng { foreach (a[i]) a[i] < 5; }
      constraint c_uq  { unique { a }; }
    endclass

    Hint

    Each element comes from 0..4; unique means all four elements differ.

    Step-by-step solution

    Diagram
    1) Each of the 4 elements is in 0..4 (5 values) and all must differ: 5 x 4 x 3 x 2 = 120 ordered selections (5P4).
    2) a[0]==2: choose the other three from the remaining four values: 4 x 3 x 2 = 24, so P(a[0]==2)=24/120=1/5 (by symmetry every value is 1/5).
    3) With a[i] < 3: only 3 values for 4 distinct slots, so there is no solution and randomize() returns 0.
    4) With ascending order added to the original: a strictly increasing 4-subset of {0..4} is one subset per choice of the omitted value, so C(5,4)=5 solutions; P(a[0]==0)=4/5.

    Answer

    Answer: 120 solutions, P(a[0]==2)=1/5; a[i]<3 is unsatisfiable (randomize fails); with ascending order only 5 solutions remain.

    How this was checked

    Checked by running a model of the rule in a Python model and Verilator 5. Exact enumeration of the constraint set per the LRM (the LRM defines the distribution; simulator solvers need not be uniform). Enumerates the 4-element arrays over the allowed values with all elements distinct (pigeonhole when fewer than 4 values exist). Verilator 5.048: the unique array always solves with four distinct values from 0..4, the ascending variant solves and is ascending, and a[i]<3 returns 0.

    Why candidates get it wrong

    unique is a pigeonhole constraint: four distinct values need at least four available values. Narrowing the element range to 0..2 does not make fewer solutions, it makes zero (and randomize() returns 0). Verified on Verilator 5.048: the original, a[0]==2 and ascending versions all solved (ascending gave 1 2 3 4) and the a[i]<3 variant failed; counts themselves are exact enumeration.

    Interviewer follow-up

    With a[i] < 4 instead of < 5, how many solutions are there and what is P(a[0]==0)? Model answer: four distinct values from {0,1,2,3} fill four slots, so the array is a permutation: 4! = 24 solutions and P(a[0]==0)=6/24=1/4.

    Say it out loud

    Four unique elements from five values, zero to four, gives five times four times three times two, which is 120 solutions, and by symmetry the probability that a[0] is two is one fifth. If the bound becomes less than three there are only three values for four distinct slots, so there is no solution and randomize returns zero. With ascending order added, there are only five solutions, one for each omitted value. The trap is thinking a narrower range gives fewer solutions; unique is a pigeonhole constraint, so it gives none. Next I would check the range is at least as large as the array.

    Related topics

    • unique Constraints

    • foreach Constraints

    • Back to randomize() with Puzzles

    • Randomization & Constraint Puzzles section

    Previous

    106 / 293

    Next: constraint_mode Changes Pass/Fail

    Up next

    Up nextconstraint_mode Changes Pass/Fail

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Inline Implication That Looks Like a Conflict
    • rand_mode(0) Freezes Value and Can Overconstrain
    • pre_randomize Assignment vs Solver and post_randomize Call Count