VLSI DV Interview Puzzles · All levels
Queue Mutation Plus Dynamic Resize
Compute final `q`, `deleted`, and `da` after all operations.
Puzzle
Difficulty: Easy · Puzzle 1 of 6 · Topic: Queue and Array Puzzles
Compute final `q`, `deleted`, and `da` after all operations.
Code
systemverilog
module p1;
int q[$] = '{1,2,3};
int da[] = '{10,20,30,40};
int deleted;
initial begin
q.push_front(9); // {9,1,2,3}
q.insert(2, 77); // {9,1,77,2,3}
deleted = q.pop_back(); // deleted=3, q={9,1,77,2}
q.delete(1); // {9,77,2}
da = new[2](da); // {10,20}
da = new[5](da); // {10,20,0,0,0}
$display("q=%p deleted=%0d da=%p", q, deleted, da);
end
endmoduleHint
Queue methods mutate immediately; dynamic-array `new[N](old)` copies only prefix `min(old.size, N)`.
Step-by-step solution
diagram
1) Queue operations produce `{9,77,2}` and popped value 3.
2) Resizing to 2 truncates dynamic array to first two elements.
3) Resizing to 5 extends that truncated array and default-initializes new slots to 0.Answer
Answer: q=' {9,77,2}, deleted=3, da=' {10,20,0,0,0}
Why candidates get it wrong
People expect second resize to recover old truncated elements, but they are already lost.
Interviewer follow-up
How can you preserve full original data before truncating a dynamic array?