Amazon Interview Question for Software Engineer / Developers






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

it's classic reservoir sampling problem, say space limit is N,
first fill sentences into the space until full, then start from N+1 element in stream, get random number between 1 and N+1, and if the number is smaller than N,
then put this one into space, and we also have to randomly remove one that is already in space by get a random number between 1 and N, then keep doing this till stream exhausted.

- Raynor March 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But we have to choose one sentence finally.. so shd we make N i.e available space as 1 ? as we cannot choose 1 from the last remaining N because then it is not making it random across all sentence? Please correct me if, I am wrong.

- pk March 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am not sure and unable to understand how your solution would be as good as returning any random (w.r.t the entire stream of sentences) as this solution expects ?

- Anonymous March 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think Reservoir sampling will work for this problem if we set N as 1. Suppose there are total m sentences, the possibility for i-th sentence being chosen is :
1/i x i/(i+1) x (i+1)/(i+2) x ... x (m-2)/(m-1) x (m-1)/m = 1/m

- Yokuki March 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yep Yokuki is right, set N to 1 here then reservoir sampling will work, and the possibility for every sentence to be chosen is 1/size of the stream

Thanks:)
Raynor

- raynor08 March 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1st iteration-
temp = read 1st sentence;
i=2;

while((sentence =  read i-th sentence from stream) != null) { //keep reading till stream is exhausted
     if random() > i/(i+1){
          temp = sentence;
     }
}

return temp;

- answer March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, forgot to do i++ just before "while" ends

- answer March 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Good question

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

what is random() returning here....decimal or whole number?
If whole number how would this logic work?
for instance if random() is returning 7 and if we have 10 lines in stream we expect the logic to return 7th line from the stream.
This logic would return 10th line, becos for every iteration always random() > i / (i+1)is true. ( 7 > 2/3 , 7 > 3/4, 7 > 4/5 and so on...)

- krish March 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

random() returns a number between 0 to 1

- answer March 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi,
Can you please also mention the amazon location where this question was asked?

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

interview was for amazon-Seattle (Kindle group)

- answer March 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we see each sentence in the stream one after the other, as in we first see the 1st then the 2nd .....the i'th and so on.

to get a random sentence, accept the current sentence as sample with probability (1/i)...this will give us a random sentence with constant space requirement.

- intervwn@_amazn_2mrow April 14, 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