System Verilog interview question

Hi,

Can someone help me with the code for the below question.
This is one of the interview questions asked to a friend.

Given an integer n, create an array such that each value is repeated twice.
For example;
n=3->[1,1,2,2,3,3]
n=4->[1,1,2,2,3,3,4,4]

After creating it, find a permutation such that each number is spaced in such a way that they are at a “their value” distance from the second occurrence of the same number.

For example: n=3 → This is the array - [1,1,2,2,3,3]

Your output should be [3,1,2,1,3,2]

The second 3 is 3 digits away from the first 3.
The second 2 is 2 digits away from the first 2.
The second 1 is 1 digit away from the first 1.

Return any 1 permutation if it exists.
Empty array if no permutation exists.

Thanks in advance.

1 Like

In reply to sk7799:


class fs_array#(parameter int N);
//int N=7;
  rand bit[2:0] d_array[];
  
  constraint d_size{d_array.size==2*N;}
  
  constraint c1{foreach(d_array[i])
    if((i>0) && (i%2 !=0))
      d_array[i-1]==d_array[i];
     
             }
  
  constraint c2{foreach(d_array[i])
               if(i>0 &i%2!=0)
                 d_array[(2*N)-1] ==N;}
  
  constraint c3{foreach(d_array[i])
    if(i<=1)
    d_array[i]==1;
               }
  
  constraint c4{foreach(d_array[i])
    if(i>1 & i%2!=0 & i<((2*N)-1))
      d_array[i]==d_array[i-2]+1;
               }
  
  function void display();
    $display("contents = %p",d_array);
endfunction
  
  
endclass

module tb;
  fs_array#(5) f_h;
 
  initial begin
    f_h = new();
    if (f_h.randomize)
    f_h.display();
  end
 
 
endmodule

contents= '{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}

i gave it a try but it works only for N=3 :)

class fin_array;
  int N = 3;
  rand int arr1[];
  rand int arr2[];
  
  constraint c1 {arr1.size() == 2*N;}
  constraint c2 {arr2.size() == 2*N;}
  
  constraint c3 {
    // repeat each item twice
    foreach(arr1[i])
      if(i%2==1) 
        arr1[i] == arr1[i-1];
      else
        arr1[i] == (i/2)+1;
  }
   
  constraint c4 {
    foreach(arr2[i])
      arr2[i] inside {arr1}; 
    
    // make sure arr2 has only 2 items of each number
    foreach(arr2[i])
      arr2.sum(d1) with (int'(d1==arr2[i])) inside {0,2};  // had to use 0 to deal with repeated items
    
    // set distance between repeating numbers
    foreach(arr2[i])
      if ((i + arr2[i] + 1) < arr2.size()) // to avoid overflow
        arr2[i] == arr2[i + arr2[i] + 1];
  }
endclass

module top;
  fin_array a1;
  initial begin	
    a1= new();
    a1.randomize();
    $display("array1=%p array2=%p", a1.arr1, a1.arr2);
  end
endmodule 

run
array1=‘{1, 1, 2, 2, 3, 3} array2=’{2, 3, 1, 2, 1, 3}

In reply to uvmuser007:

actually it’s failing for N== 4… may i know the reason?

@Desam Is there any mathematical solution for N=4?
There is no combination to satisfy it

How about this code

I am bit confused about the second part . can you provide more clarity ?
For example: n=3 → This is the array - [1,1,2,2,3,3]

Your output should be [3,1,2,1,3,2]

The second 3 is 3 digits away from the first 3.
The second 2 is 2 digits away from the first 2.
The second 1 is 1 digit away from the first 1.

class my_class;
  rand int array[];
  rand int val;

  constraint c0 {val inside {[3 : 10]};}
  constraint c1 {array.size() == val * 2;}

  constraint c2 {
    foreach (array[i]) {
      if(i<2){
        array[i]==1;
      }else{
        if (i % 2 == 1) {
          array[i] ==array[i-2]+1;
          array[i-1] == array[i-2]+1;
        }
      }
    }
  }
endclass

module top ();
  my_class m1;
  initial begin
    m1 = new();
    repeat (5) begin
      m1.randomize();
      $display("Val = %0d ; Array_size=%0d; Array = %p", m1.val,m1.array.size(),m1.array);
    end
  end
endmodule

result

# Val = 7 ; Array_size=14; Array = '{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}
# Val = 7 ; Array_size=14; Array = '{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}
# Val = 9 ; Array_size=18; Array = '{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9}
# Val = 8 ; Array_size=16; Array = '{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}
# Val = 5 ; Array_size=10; Array = '{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}