Amazon Interview Question for Software Engineer / Developers






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

anyone got the solution?

- Andy July 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fun(int a,int b)
 {
  int x=0;
  while(b)
   {
    b=b>>1;
    x++;
   }
   return utility(a,b,x);
 }
 
 
 int utility(int a,int b,int x)
  {
   int y=0;
   for(int i=0;i<x;i++)
    {
     y=y|random();
     y=y<<1;
    }
    if(y>=b)
     return utility(a,b,x);
    else if(y<a)
    return 0;
    else
     return 1;
  }

- Erik July 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nicely Done.

- Yan July 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please explain this algo little bit?

- rocky July 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very nice implementation, Erik!

- Alice July 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how I will implement it:

Take the greater of two numbers in this case b. Generate a random no between range [1,b] if this no is less than equal to a then output heads else output tails. So, if choose a = 5 and b =7 generate no between 1 and 7. If no is less than equal to 5 output heads. The probability of generating heads here is 5/7 i.e. a/b and probability of generating tails is 2/7 i.e. (b-a)/b

- Anonymous July 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how I will implement it:

Take the greater of two numbers in this case b. Generate a random no between range [1,b] if this no is less than equal to a then output heads else output tails. So, if choose a = 5 and b =7 generate no between 1 and 7. If no is less than equal to 5 output heads. The probability of generating heads here is 5/7 i.e. a/b and probability of generating tails is 2/7 i.e. (b-a)/b

- gauravk.18 July 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, Erik's solution does the same thing.

If you noticed, we are only given a _fair_ coin, i.e 1 with pbt 1/2 and 0 with pbt 1/2. So the statement "I will generate a random number in {1,2,..,b}" needs to make use of the fact that the only source of randomness you have is a fair coin.


What Erik is doing is to generate [log b] random bits of a number. If that number (as represented in binary by those bits) is greater than b, he tries again.

if that number is <= b and <=a , then it is heads, else if that number > a and <=b, it is tails.

- LOLer July 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did not see the other solution just proposed how I think it can be done but thanks for the explanation

- Anonymous August 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, frankly, what you said is the easy part of the problem and isn't of much help.

The harder part is actually generating a random number in {1,2,...,b} using a uniformly random bit generator.

- LOLer August 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not sure if I understand the problem fully, bit is this sufficient?

int unevenRand (unsigned int a, unsigned int b)
{
	unsigned int number;
	rand_s(&number);
	number = number%(b);
	if (number <a)
		return 1;
	else
		return 0;
}

- restlesstoday August 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not sure if I understand the problem fully, bit is this sufficient?

int unevenRand (unsigned int a, unsigned int b)
{
	unsigned int number;
	rand_s(&number);
	number = number%(b);
	if (number <a)
		return 1;
	else
		return 0;
}

- restlesstoday August 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is insufficient.

The point is to implement the random number generator, given that fact that your only source of randomness is a random bit generator.

- LOLer August 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think Ericks solution is the simplest one also gaurav.18 explanation says he is doing the same thing. that it... very nice question and nicely answered by Eric. Thanks Erick.

- LLOLer August 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think gaurav missed the whole point of the question. And so did restlesstoday. Apparently you missed it too, if you think gaurav got it.

- LOLer August 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

While Eric's solution is perfect, I would imagine there are more ways to generate a random number between 0 & b. For instance grab the current time and in particular seconds and then perform (%b) operation it. This may provide a random number between 0 & b . Any suggestions ?

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

second to LOLer,

The point is to implement the random number generator, given that fact that your only source of randomness is a random bit generator.

- durbin October 03, 2009 | 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