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. Q&A: Arrays & Nested Objects

Part 3 · Constraint Randomization · Intermediate

Q&A: Arrays & Nested Objects

Revision: length fields, guarded element rules, 2D loops, wide sums and null handles.

On this page

    Why this matters

    A payload test constrained all 64 slots and failed on short packets. The rules had no length guard, so they constrained bytes that were never sent.

    After this lesson you can: answer the array questions interviewers ask: length fields, guards, 2D loops, wide sums and null handles.

    Note

    Revision page. Answer each question before opening it; the full lessons are Constraining Array Size, foreach Constraints, sum(), Aggregate Constraints, Arrays of rand Objects.

    Predict first Why pair a fixed array with a rand len field instead of using a dynamic array?

    len solves together with the elements, and an i < len guard limits the rules to the used slots. A dynamic array's size is solved first, and Verilator 5.048 could not combine size() with sum() on one.

    Predict first How do you loop over every cell of m[4][4] in a constraint?

    foreach (m[i, j]), the standard's comma form. m[i][j] in the loop header is not standard syntax, and Verilator rejects it.

    Predict first Ten bytes must be different and total 100. What two rules?

    arr.sum() with (int'(item)) == 100 and the pairwise rule if (i < j) arr[i] != arr[j]. Ten different bytes reach at least 0 + 1 + ... + 9 = 45, so 100 is feasible.

    Lengths, guards and loops

    Construct

    Rule

    Trap it avoids

    len field plus guarded foreach

    len and elements solve in one call

    Sizing first, then filling by hand

    Dynamic size()

    Solved first, then the element rules

    A size range with no legal elements

    Comma form m[i, j]

    One loop over both dimensions

    m[i][j], which some tools reject

    Guarded element rule

    Rules apply only below len

    Constraining garbage past len

    Handles, sums and uniqueness

    A null child handle is never built by the solver: the constructor owns allocation. Sums widen first, and uniqueness that must work on every tool is written pairwise.

    The bench checks the length guard and the 2D loop over 200 frames, and ten different bytes totalling 100 over 200 calls.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class frame;
      rand bit [7:0] len;
      rand bit [7:0] pay[16];
      rand bit [7:0] m[4][4];
      constraint c_size { len inside {[1:16]}; }
      constraint c_pay  { foreach (pay[i]) if (i < len) pay[i] inside {[1:50]}; else pay[i] == 0; }
      constraint c_2d   { foreach (m[i, j]) m[i][j] inside {[0:15]}; }   // comma form for 2D
    endclass
    
    class pkt_sum;
      rand bit [7:0] arr[10];
      constraint c_sum  { arr.sum() with (int'(item)) == 100; }          // widened
      constraint c_pair { foreach (arr[i]) foreach (arr[j]) if (i < j) arr[i] != arr[j]; }
    endclass
    
    Terminal
    frame: len 7, guarded payload and 4x4 matrix legal over 200 calls
    pkt_sum: ten different bytes totalling 100, e.g. '{'h6, 'h7, 'h2e, 'h11, 'hc, 'h2, 'h5, 'h0, 'h4, 'h1}
    PASS: guarded rules, the 2D comma loop, the widened sum and pairwise uniqueness all held

    Common pitfall

    max() and min() are locator methods that return queues, so they do not belong in constraints: bound every element with foreach instead. sum() over a comparison is the workhorse for counting.

    Interview

    Senior phrasing for this round: a length field solves with the elements while a dynamic size() solves first, null handles are construction bugs, not solver bugs, and every sum widens first.

    Remember

    • A fixed array plus a rand len solves in one call.

    • foreach (m[i, j]) is the standard 2D form.

    • Widen sums; write uniqueness pairwise when it must work everywhere.

    Rule of thumb: guard every element rule with the length that decides which slots are real.

    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.

    Related topics

    • Next: Assertions (SVA)

    Previous

    100 / 259

    Next: SystemVerilog Assertions — Interview-Critical Deep Dive

    Up next

    Up nextSystemVerilog Assertions — Interview-Critical Deep Dive

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Even/Odd, Alignment & Divisibility
    • Powers of 2, One-Hot & Bit Games
    • Unique Values & Ordering