Algorithms
Can you create two distinct arrays, each with 10 unique elements?
Design Verification Engineer
Meta
Adobe
Akamai
IBM
Lockheed Martin
NVIDIA
Answers
Anonymous
6 months ago
class abc;
rand int a[10];
rand int b[10];
constraint c1{foreach(a[i]) a[i] inside{[1:100]};
unique{a[i]}; }
constraint c2{foreach(b[i]) b[i] inside{[1:100]};
b[i]!=a[i];}
function void post_randomize();
$display("a=%d, b=%d", a, b);
endfunction
endclass
module tb;
abc num=new();
initial begin
num.randomize();
end
endmodule
Anonymous
9 months ago
constraint c {
a.size == 10;
b.size == 10;
unique {a,b};
}
Unique key word makes sure that both a and b have unique elements wrt each other as well. If we want to have some elements common in both a and b, we can put unique in different unique statements. Repetition is not guaranteed, but possible.
Interview question asked to Design Verification Engineers interviewing at Northrop Grumman, Meta, Fujitsu and others: Can you create two distinct arrays, each with 10 unique elements?.