Microsoft Interview Question for Software Engineer / Developers






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

double computeAverage(double newVal) {
static int count=0;
static float average=0;
average = (average * count + newVal)/++count;
return average;
}

Every time the average is computed from the previous average and count. count is the number of values seen so far. Since we need the average and count to persist between function calls, we can declare them static.

- srihari January 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is correct logically but for the initialization steps.
You are reinitializing the static sount and average to zero which makes you lose the previous values.

So you need to remove " =0 " in the first 2 lines

- shruti April 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

shruti don't you think making them static avoids reinitialization ! ?

- Anonymous September 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Instead of static average, use static sum. If I'm not mistaken, the idea is to accumulate the numbers and then return the average.

double computeAverage(double newVal)
{
static int count=1;
static double sum=0;
sum+=newVal;
return sum/count++;
}

I believe a float allows a smaller mantissa so you may have a buffer overflow sooner.

- Jack January 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(previous average* previous number+new number)/(previous number+1)

- Anonymous March 04, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use static average and calculate the next average = (average + newVal)/2.0;

- anon July 18, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

anon,

you are wrong Consider[1,2,3]
Actual avg -> 2
avg by your method ->
(1+2)/2 = 1.5
(1.5 + 3)/2 = 2.25 (WRONG!!!!!!)

- TheGreatOne September 08, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Look at this table:

Total nos seen Num Avg
1 2 2
2 4 3= prev avg+(4-prev no)/nos seen so far
3 6 4=3+(6-3)/3

Got it!!!

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

in C++ this worked for me
double computeAverage(double newVal){
static int cnt;
double avg=newVal/++cnt;
return avg;
}

- prolificcoder April 13, 2009 | 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