VLSI DV Interview Puzzles · All levels

Queue Assignment Copies by Value

After assignment and independent mutations, what are `q1` and `q2`?

Puzzle

Difficulty: Easy · Puzzle 2 of 6 · Topic: Queue and Array Puzzles

After assignment and independent mutations, what are `q1` and `q2`?

Code

systemverilog
module p2;
  int q1[$] = '{1,2,3};
  int q2[$];
  initial begin
    q2 = q1;
    q1.pop_front();
    q2.push_back(4);
    $display("q1=%p q2=%p", q1, q2);
  end
endmodule

Hint

Queues are unpacked value types, not class handles.

Step-by-step solution

diagram
1) `q2 = q1` copies queue contents into a separate queue.
2) `q1.pop_front()` changes only q1 to `{2,3}`.
3) `q2.push_back(4)` changes only q2 to `{1,2,3,4}`.

Answer

Answer: q1=' {2,3}, q2=' {1,2,3,4}

Why candidates get it wrong

Candidates sometimes project class-handle aliasing behavior onto queues.

Interviewer follow-up

What collection type in SV would alias by handle instead of value?

Related topics