## Qualcomm Interview Question

Software Engineer in Testsfor filling an array without repeating random number:

int min=0;

int max=0;

for i 0 to 99

arrayname[i]= min + (int)(Math.random*(max-min+1))

min++;

max++;

rof

For second question you can use random function and use time as seed to generatr 1 to 100 unique numbers check each time if you have already used that number...

Observe that a matrix needs to be generated such that it a permutation of 1..100. Let us call is PI(1..100), if you generate a permutation of 1..100 and populate the matrix in any fashion you wish then you get a random matrix of 1..100.

To generate a random permutation of 1..100, the probability of generation of a particalr permutation say p should be = 1/ (100) !

that is pr { PI=p} = 1/100! for all p in PI(1..100)

`Pr{PI=p}= 1/100! = 1/100 * 1/99 * 1/98 .......* 1/2 * 1`

therefore you should be able to generate the first element with probability 1/100

second with 1/99 and so on and that would make p a random permutation.

To do this job initialize an array A={1,2,.......100}

generate an index between 0 to 99 using random function of ur choice

and choose A[index] now do a compaction of the array form index+1 to 99

so that the array has all elements in 1..100 except A[index] and array

elements occupy positions 0...98

repeat the above proc again this time generating random numebr between 0 to 98.

static int matrix[25] = { 1, 2, ... 25 };

void draw(int * pRandom1, int * pRandom2)

{

*pRandom1 = rand();

*pRandom2 = rand();

//NOTE, make sure the random1 and random2 are not identical.

}

void GenMatrix(const int MaxDrawTimes)

{

int r1, r2;

for (int i =0 ; i < MaxDrawTimes; ++i)

{

//Exchange eletements according to random draw

draw(&r1, &r2);

matrix[r1] <===> matrix[r2];

}

}

int main(int argc, char* argv[])

{

GenMatrix(rand());

//Print out generated matrix.

return 0;

}

Can you please elaborate on this question? Bcoz What I am guessing is you are trying to tell that Build a 5x5 matrix from 1-25 where each row,column and diagonals have same sum? Am I right?

- Vishrut April 04, 2011