Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

store avg. and count of numbers
do (avg/(count+1))* count + newelement / (count+1)
to avoid integer overflow

- Siva June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please explain howz this working?

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

Shouldn't this be:
(avg/(count))* (count+1) + newelement / (count+1)

- luv June 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no.siva is correct

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

You have to be careful with solutions like this. Storing the average in some sort of floating-point value runs the risk of accumulated roundoff error. It would be important to clarify precision requirements first.

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

This works because by definition
{{
avg = (1/n) * sum(i=0 to n) a[i]
}}
Then
{{
n * avg = sum(i=0 to n) a[i]
}}
Hence
{{
n * avg + a[n + 1] = sum(i=0 to n) a[i] + a[n + 1] = sum(i = 0 to n+1) a[i]
}}
so that
{{
(n * avg + a[n + 1]) / (n + 1) = (1 / (n + 1)) sum(i = 0 to n+1) a[i]
}}
which is the new average. The trick is to not store the sum of the numbers seen so far because it can blow up. However, you pay for that with roundoff errors.

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

You could store the numbers precisely if you wanted to, and it wouldn't be all so bad. If your incoming numbers are d digits long, you would need d*logN bits for the exact sum, and another logN for the counter, so O(d*logN) total space. Calculation time would be at worst something like the square of that (d^2*(logN)^2), so still very reasonable.

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

Keep Avg and count of numbers so far
When new number comes
count++;
tempNum = Num - avg;
tempNum = tempNum / count;
Avg = Avg + tempNum;

- algoFreak June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 4 vote

current_avg = prev_avg + (current_element - prev_avg)/(prev_count+1)

- mshsayem June 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(1) Take three variables avg,count=0;
for first element store it in avg and increment count by 1
for each coming element
avg=(avg*count+ComingElement)/(count+1);
done
print avg
exit

- Vineet Setia June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"avg*count" this is an unnecessary overhead. instead u cud just add all and divide by count

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

Vineet Setia is right.

- God June 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi..
Sol=(ABS(PrevAvg-CurrentNum)/(prevCount+1))+prevAvg.
Reason for this solution is even if you multiply the prevAvg with count there is a chance of overflowing.
Correct me if I do wrong..because this is my first post.

- Y Praveen Kumar June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

take average of some n numbers ( where n is not too large say n=5)
then take average of next n numbers
now take average of these two averages.

now get avg of next n numbers and so on.....

- krishnakanth June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

earlier average (with n elements) + ((new element - earlier average)/ (n+1))

- Anonymous June 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

count = 1, avg=0;
while((input = getInput()) != EOF)
{
avg = (avg*(count-1) + input)/count;
count++;
}

- JustStarted June 15, 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