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. pre_randomize Assignment vs Solver and post_randomize Call Count

VLSI DV Interview Puzzles · All levels

pre_randomize Assignment vs Solver and post_randomize Call Count

Predict outcomes of both calls: final len range for Call A and whether post_count increments on Call B.

On this page

    Puzzle

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

    Predict outcomes of both calls: final len range for Call A and whether post_count increments on Call B.

    Code

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class life_req;
      rand bit [3:0] len;
      bit force_short;
      int post_count;
      constraint c_base { len inside {[1:8]}; }
    
      function void pre_randomize();
        if (force_short) len = 2;
      endfunction
    
      function void post_randomize();
        post_count++;
      endfunction
    endclass
    
    life_req r = new();
    r.force_short = 1;
    assert(r.randomize() with { len inside {[5:6]}; }); // Call A
    void'(r.randomize() with { len == 9; });            // Call B

    Hint

    pre_randomize runs before solving; post_randomize runs only on success.

    Step-by-step solution

    Diagram
    1) Call A: pre_randomize assigns len=2, but solver then enforces c_base and inline len in 5..6.
    2) So Call A succeeds with len either 5 or 6; tuple-count gives 50/50.
    3) post_randomize runs after successful Call A, so post_count increments by 1.
    4) Call B asks len==9, conflicting with c_base len 1..8 -> fail.
    5) Because Call B fails, post_randomize is not invoked for that call.

    Answer

    Answer: Call A succeeds with len in {5,6} (about 50/50), then Call B fails, and post_count increments only once total.

    Why candidates get it wrong

    Candidates often think pre_randomize assignment pins the rand value and that post_randomize runs even on failed randomize.

    Interviewer follow-up

    How would you intentionally lock len to 2 for Call A without causing solve failure?

    Related topics

    • Back to randomize() with Puzzles

    • Randomization & Constraint Puzzles section

    Previous

    103 / 210

    Next: Randomization & Constraint Puzzles: Tricky Q&A

    Up next

    Up nextRandomization & Constraint Puzzles: Tricky Q&A

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Inline Implication Can Empty Intersection
    • if-else in with Clause
    • foreach Inline Constraint on Dynamic Array