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
    • 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
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • Interview Prep: Puzzle Rounds5
  • 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

VLSI DV Interview Puzzles · All levels

Associative Array Iteration Order

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

On this page

    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

    Why candidates get it wrong

    People often expect associative iteration to follow insertion order like many software maps.

    Interviewer follow-up

    What does `aa.next(k)` return if called once more after `k=b`?

    Related topics

    • Back to Queue and Array Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    21 / 210

    Next: Locator Methods Return New Queues

    Up next

    Up nextLocator Methods Return New Queues

    Sign in to save progress

    Practice this lessonReport an error

    See also

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