Interview Question






Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried:

Round(7 * (rand()/rand()))

- shassant2 December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

7 * (rand()/rand()) is not right.
There's someone have posted, as far as I know, you may make two succesive call of rand()to produce a number. so you get a random number from 1 to 25, the range is larger than [1,7]. if the number come out is within [8,25] you can simply ignore it.
int getRand() {
int num = 0;
do {
num = (rand()-1)*5+rand();
}while(num > 7);
return num;
}
of course, you are recommented make 5, 7 as parameter of the fuction #getRand().

May you good luck!
/Dam775

- dam775 December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about - rand() + rand()%3 (% is modulo operation)?

- Dumbo December 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is nice but the issue is it would never return 1.

- saurabh December 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

rand() + ( (rand()-1)/2 ) , this will work i think.

- Balaji Yalamarthi December 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

another combination could be:
let the function is
Rand1to5() // it returns random number between 1 and 5;
then,
Rand1to7 = Rand1to5()+ceil(Rand1to5 * 3 / 5) - 1; //this will always return desired random number

- Abhishek December 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about dividing the result by 5 and multiplying by 7

- Anonymous March 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

None of the ideas above would work because implicit in the question is the fact that the output should be uniformly distributed.

But using rand5, you can get a 0 or a 1 uniformly dist. (if (rand5=1 or 2) output 0, if(rand5=3 or 4) output 1, else try again). calling this 3 times you can get any number from 0 to 7 (in binary). Just ignore the 0 (i.e. generate again)

This would do it. Not the most elegant solution, but WTF.

- memo June 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the numbers are not necessarily integers.

- Victor P. August 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

float a = Rand(); //uniform(1-5)
float b = Rand()/5; //uniform(0-1)
float c = Rand()/5; //unoform(0-1)
if (c>= 1/2) a = a+b;
else a = a-b;

- Victor P. August 18, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More