Amazon Interview Question for Software Engineer / Developers

• 0

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

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

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

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

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

no.siva is correct

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

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.

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.

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

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.

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;

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

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

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

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

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

Vineet Setia is right.

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.

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.....

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

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

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++;
}

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.

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.