Q273FreeSystemVerilog
Return the first unique value in a stream
Interview prompt
Question
Process a stream of integers and return the earliest value whose current occurrence count is exactly one.
Starting point
Question code
class FirstUnique;
function void push(int x);
function bit first_unique(output int x);
endclassReviewed example
Trace one case
Input
push sequence=[2,3,2,4,3]Expected output
first unique after each push=[2,2,3,3,4]Duplicated values remain in the order queue until they reach its head, where lazy cleanup exposes the earliest surviving singleton.
What to cover
Requirements
- Increment a count for every push.
- Append a value to the order queue only on its first occurrence.
- Lazily remove duplicated values from the queue head.
- first_unique(x) returns 0 when no unique value remains and leaves x unspecified in that case.

