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

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • 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 Can Empty Intersection
      • if-else in with Clause
      • foreach Inline Constraint on Dynamic Array
      • 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
  • Interview Prep: Puzzle Rounds5
  • DV Puzzle Cheatsheet
Loading lesson

‹randomize() with Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. Randomization & Constraint Puzzles
  4. randomize() with Puzzles
  5. foreach Inline Constraint on Dynamic Array

VLSI DV Interview Puzzles · All levels

foreach Inline Constraint on Dynamic Array

Does this randomize call have one solution or many? State the exact resulting n and data.

On this page

    Puzzle

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

    Does this randomize call have one solution or many? State the exact resulting n and data.

    Code

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class payload_req;
      rand int unsigned n;
      rand byte data[];
      constraint c_base {
        n inside {[2:4]};
        data.size() == n;
      }
    endclass
    
    payload_req r = new();
    assert(r.randomize() with {
      n == 3;
      foreach (data[i]) data[i] == i;
    });

    Hint

    n fixes size first; foreach fixes each element exactly.

    Step-by-step solution

    Diagram
    1) Inline constraint fixes n=3.
    2) data.size()==n gives size 3.
    3) foreach imposes data[0]=0, data[1]=1, data[2]=2.
    4) Exactly one legal assignment exists.

    Answer

    Answer: Deterministic outcome: n=3 and data='{0,1,2}; success probability is effectively 100% because the set is satisfiable and unique.

    Why candidates get it wrong

    Some assume foreach introduces randomness by itself; equality in foreach can make the result fully deterministic.

    Interviewer follow-up

    How would you tweak foreach to keep n=3 but allow 8 distinct data combinations?

    Related topics

    • Back to randomize() with Puzzles

    • Randomization & Constraint Puzzles section

    Previous

    100 / 210

    Next: constraint_mode Changes Pass/Fail

    Up next

    Up nextconstraint_mode Changes Pass/Fail

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Inline Implication Can Empty Intersection
    • rand_mode(0) Freezes Value and Can Overconstrain
    • pre_randomize Assignment vs Solver and post_randomize Call Count