VLSI DV Interview Puzzles · All levels

Pop from Empty Queue

What values are returned by `pop_front` and `pop_back` on an empty queue?

Puzzle

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

What values are returned by `pop_front` and `pop_back` on an empty queue?

Code

systemverilog
module p3;
  int q[$];
  int a, b;
  initial begin
    a = q.pop_front();
    b = q.pop_back();
    $display("a=%0d b=%0d size=%0d", a, b, q.size());
  end
endmodule

Hint

SV returns default value of element type when popping from an empty queue.

Step-by-step solution

diagram
1) Queue starts empty.
2) `pop_front()` returns default `int` value 0 when queue is empty.
3) `pop_back()` on still-empty queue also returns 0.
4) Queue size remains 0.

Answer

Answer: a=0, b=0, size=0

Why candidates get it wrong

Some interviewees expect runtime fatal behavior and skip default-value semantics.

Interviewer follow-up

How would behavior differ for `string q[$]` when popping empty?

Related topics