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
    • Datatype and Bit Puzzles
    • Fork Join Puzzles
    • Queue and Array Puzzles
      • Queue Mutation Plus Dynamic Resize
      • Queue Assignment Copies by Value
      • Pop from Empty Queue
      • Dynamic Array Resize with Class Handles
      • Associative Array Iteration Order
      • Locator Methods Return New Queues
    • String and Casting Puzzles
    • SystemVerilog Language Puzzles: Tricky Q&A
  • 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
  • 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

←Queue and Array Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. SystemVerilog Language Puzzles
  4. Queue and Array Puzzles
  5. Associative Array Iteration Order
On this page

    VLSI DV Interview PuzzlesAll levels

    Associative Array Iteration Order

    Predict outputs of `first`, `next`, `num`, and `exists`.

    Puzzle

    Difficulty: Medium · Puzzle 5 of 6 · Topic: Queue and Array Puzzles

    Predict outputs of `first`, `next`, `num`, and `exists`.

    Code

    p5.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    module p5;
      int aa[string];
      string k;
      bit ok;
      initial begin
        aa["b"] = 2;
        aa["a"] = 1;
        ok = aa.first(k);
        $display("first_ok=%0d k=%s", ok, k);
        ok = aa.next(k);
        $display("next_ok=%0d k=%s", ok, k);
        $display("num=%0d ex_c=%0d", aa.num(), aa.exists("c"));
      end
    endmodule

    Hint

    Associative arrays iterate in index order, not insertion order.

    Step-by-step solution

    Diagram
    1) Keys are `"a"` and `"b"`; lexical order gives `"a"` first.
    2) `first(k)` returns 1 and sets k to `a`.
    3) `next(k)` from `a` returns 1 and sets k to `b`.
    4) `num()` is 2 and `exists("c")` is 0.

    Answer

    Answer: first_ok=1 k=a, next_ok=1 k=b, num=2 ex_c=0

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. String keys iterate in lexical order; Icarus 13 does not support string-indexed associative arrays.

    Why candidates get it wrong

    Associative arrays iterate in key order (strings compare lexicographically), not insertion order, and `next` on the last key returns 0 and leaves the key variable unchanged.

    Interviewer follow-up

    What does `aa.next(k)` return if called once more after `k=b`? Model answer: 0, because there is no next key, and `k` stays "b" (run).

    Say it out loud

    I would remember that an associative array walks its keys in sorted order, not insertion order, and strings compare lexicographically. So first gives a, next gives b, there are two entries, and key c does not exist. The trap is that next on the last key returns 0 and leaves the key variable unchanged. I would check whether the code is wrongly relying on insertion order.

    Related topics

    • Associative Arrays

    • Back to Queue and Array Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    27 / 293

    Next: Locator Methods Return New Queues

    Up next

    Up nextLocator Methods Return New Queues

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Queue Mutation Plus Dynamic Resize
    • Queue Assignment Copies by Value
    • Pop from Empty Queue