Amazon Interview Question for Software Engineer / Developers






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

1)
here is how it can be done

int count=0;
char *winner="none";
char *curName="";
while(curName=readNextNameFromFile()!=EOF){
  count++;
  int random=getRandom(1,count) // in the range of 1 .. count
  if(random == count) winner=curName;
}

the idea is, at every new name read, pick either current winner or new name as winner, with probabilities in proportion to (n-1/n):1/n

this will ensure that all the names read till count = n will have equal chance (1/count) of getting picked as winner.

2) i would go for HashSet.

3) XOR would be fastest.

- akshay kumar June 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

don quite agree with the first. This alogrithm may generate no winners at all.

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

count=1;
while(end of the file)
{
//get one record at a time
if((random(count)+1 == count))winner = current record;
count++;
}

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

For third, can we go for...
Compare first 3 numbers, see which is occuring 3 or 2 times.
If one number is occuring 2 times, other number is the one.
Else if all 3 numbers are same, scan through array to find a number different than the current one.
Worst time complexity is O(n) and average case is O(n/2) and best case is O(1).

- Anonymous October 17, 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