Microsoft Interview Question for Software Engineer / Developers


Team: Windows Live
Country: United States
Interview Type: In-Person




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

will this work?
int randomR1R2(int R1, int R2) {
int range = R2 - R1 + 1;
int mod = (2^32 - 1) % range;
int number = -1'
do {
number = randomInt();  //randomInt()returns a random integer between 0 and integer max (2^32 - 1) on a 32bit machine
}while (number > (2^32 - 1 - mod))
return number % range +R1;

- HT January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The only problem is that you have unlimited worst case execution time.

- Anonymous January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree. But the issue is, the worst case would be range = (2^32 - 1) / 2 + 1 and then mod will be (2^32 - 1) / 2 - 1. The possibility of number > (2^32 - 1 - mod) will be around 1/2 for each loop. The possibility of a infinite loop will be extremely low after several rounds, say 20 rounds.
The possibility of loop continue will be as low as 1 / (2^20). I think the above solution will be good enough for the question. Point me out if I am wrong, thx

- HT January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you are that worried about an infinite loop just put a maximum loop limit like 100. If it hits that just return a number using the nonuniform function.

- Rayden January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I actually just more or less proposed the same solution in another comment. Oracle's JVM implementation's Random class uses this approach.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i agreee but in this types of question dat issue dat worst case wpuld be range (26^32-1)/2+1;will be around 1/2 for each loop and only unlimited worst case execution time only

- niranjan kumar January 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I would use something like that:

public uint Random(uint r1, uint r2)
{
    if (r2 < r1)
    {
        throw new ArgumentException();
    }

    uint r;
    uint range = r2 - r1 + 1;

    if (range == 0)
    {
        return Random();
    }

    uint limit = uint.MaxValue - uint.MaxValue % range;

    do
    {
        r = Random();
    }
    while (r >= limit);

    return r % range + r1;
}

The worst case is when the range is equals to uint.MaxValue/2 + 1. It means that probability of condition in the loop to be true is about 1/2. It means that expected value of Random() calls in Random(r1, r2) equals to 2. In others cases expected value will be close to 1. So number of calls inside Random(r1, r2) can be very large, but there expected value in worst case is 2 and about 1 in most cases, this is not too bad.

- Weerel February 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

can this be right?

newRandom = random()mod(R2-R1+1) + R1

- Anonymous January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes that is right and so simple too. Nice job.

- danny January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is not so simple. The result will not be equally distributed over the region. See other answers for discussion.

- tsichevski January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@tsichevski: Agreed, this is not quite correct. However, for small ranges, the distribution is almost uniform, and in fact PRNG routines have been implemented like this in the past.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Everyone is using modulus. As people pointed out, this will lead to slightly non-uniform probability.
We should use divide
x = r1 + ( random() / Int32.MaxValue ) * ( r2 - r1 )

- DarkKnight July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This always generates the same random number.

- praveen November 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is actually right ... the percentage way ... and yes Praveen it will tend to get you the same number in case the difference between R1 & R2 is not high.

- Anant Anand Gupta March 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsInRange(int R1, int R2, int random)
{
	return R1 <= random <= R2;
}

int RandomNumberGenerator(int R1, int R2)
{
	var random = Random();
	while (!IsInRange(R1, R2, random))
	{
		random = Random();
	}

	return random;
}

- foobarvin March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

int GenRand(int R1, int R2)
{
    return (R1 + rand() % (R2 - R1))
}

- nim January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

probability of the same number coming out is very high:

like R1=100 and R2=200
so for numbers like 1057,2057 ... it will return the same random number in that range.....

- saurabhroongta2 January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how about this
int GenRand(int R1, int R2)
{
    return R1 + (((double)rand() / (double)RAND_MAX) * (R2 - R1 + 1));
}

- nim January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@saurabhrungta,
The probability of other numbers will also be (almost) same...
The only difference will be for the numbers which [for max N] x=2^32-N*(R2-R1) and x>0. The probability for x will be slightly higher then others. and that will be in ration (N+1):N

- GuptaJi January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@vishnu... : There's a very simple fix. Namely, you just get another random number if you happen to be in that range that you mention. This event has prob. < 1/2 even for the worst inputs, so this fix at most doubles the running time.

I recommend readers refer to the (open-source) implementation of this kind of function in the Random class of Oracle's JVM implementation. The exact technique I describe is used.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1. Get a random number R in the range 0.. (2^32).
2. Find the minimal number N such as (2^32) mod N is zero, which is more than or equal to R2 - R1 + 1
3. Get M = R mod N
4. return M mod range

- tsichevski January 17, 2012 | 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