Amazon Interview Question for Software Engineer / Developers






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

You maintain a buffer with storage capacity of 1 line. When you see i'th line, put the i'th line in the buffer. At the end return the line stored in the buffer.

It is simple to calculate that, after having seen m lines of the text, the probability of any line being in the buffer is 1/m.

- Ajay Mishra July 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

Sorry, i missed something in the above solution, Here is one with the modification.

You maintain a buffer with storage capacity of 1 line. When you see i'th line, put the i'th line in the buffer "with probability 1/i ". At the end return the line stored in the buffer.

It is simple to calculate that, after having seen m lines of the text, the probability of any line being in the buffer is 1/m.

- Ajay July 27, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

stupid answer

- Anonymous October 24, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about maintaining a hash map of <line no., String> and then selecting it at random.

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

@ " no on Saturday" , Can you argue your comment? I think I have given a right solution, you need to work out you maths!

- Ajay July 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well Mr Ajay, i guess you need to either explain your solution well or atleast so not blame without explaining.
1. Stream of lines, would mean you will not know the number of lines.
2. What does "i" mean, where did you get "i" from what is the value of "i" ?
Try answering this , in a linked list how will you find a random node.

Start from basics when answering. If you say have a buffer and when you get the ith line put in the buffer, so does that mean i am traversing through the lines ? And i put every line in buffer , so the last line read will be in the buffer, how does that help me in getting a random line out ?

- no July 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Guys, if you don't understand something... just ask them properly. no, your sarcasm is unwarranted here. There are lots of questions on this site where people give absolutely bullshit answers. Ajay's was, in the very least, giving a faint idea of how to proceed. Yes, its true that if people were more elaborate then it would be better for all of us using these forums, but I guess we can't expect everybody to take so much pains and spoon feed all of us who require it. Ajay's definitely made sense... if it didn't to you, just ask.

- nevermind March 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Completely agree with nevermind.

In fact, Ajay's post of July 27 made sense enough to give the right answer.

People claiming that it is stupid, 'go check your answer' etc need to get a lesson in reading comprehension.

If you don't understand a solution (unless clearly wrong), ask for clarification before bashing it, at least you will sound sincere, rather than an arrogant, ignorant jerk.

And, if you think a solution is wrong, say _why_ it is wrong, rather than just saying it is wrong. I admit, in some cases, people post utter nonsense.

In this case, though, "no" has some justification for his posts before July 27. Ajay's post of July 24 was quite unclear, IMO. Why don't people at least try to understand what they wrote, before posting?

Anyway...

_rant.Off();

- T March 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

also you speak about "m" what is this "m" ? dont you need to atleast let others know what you are talking about .. i can give a better solution
take k lines and the move j lines ahead and do this p times. once you have p-j lines read then there are d lines in the p buffer with 1/k and 1/r probablity and hence a 1/stream size which is not that much probable , so we have d here, that will work. so add p+u and i will be the solution.

- no July 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If only you read carefully - I have defined m as "after having seen m lines of the text, the probability of any line being in the buffer is 1/m". and have defined i likewise.
You have a lines of text and you know how many lines you have seen till now. and you just need storage for a single line!

- Ajay July 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I still do not get Ajay's solution. If you are inserting ith line with probability 1/i then each new line will have less probability and the first line will be in buffer always. Do explain in details please.

- Anon August 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ajay's solution is correct. See problem 11(column 12) in Programming Pearls

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

dude, that's what i wanna say. i also remember a similar solution in the pearl book, about probability.
guys check this book..

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

how about this?

(1) traverse linke by line, make a count(starts from 1) and stote 1 line space
(2) replace the Nth_line to previoulsy stored line
using:
if(random(N+1)==(Nth_line))
{
line = Nth_line;
}

- mail2vcp@gmail.com October 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

good solution Ajay

- Anonymous November 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This question seems to have cropped up multiple times.

Do people even check before posting a question?

Anyway, the keywords for this problem: Reservoir Sampling.

- T March 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 0 vote

thats certainly not the right answer.

- no July 26, 2008 | 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