Flipkart Interview Question
Software DevelopersCountry: India
Before starting the running average create this:
StreamingAverage avg = new StreamingAverage();
Every time a new number is pulled from the stream, call this:
avg.addToAverage(number);
Here's the code supporting it:
public class StreamingAverage{
int count = 0;
double average = 0;
public void addToAverage(int number){
double workingAverage = this.average * this.count;
this.count ++;
workingAverage += (double)number;
this.average = workingAverage / (double)this.count;
}
public double getAverage(){
return this.average;
}
}
you can't store count because you are not allowed to sore any value more than 2 digits
this.count ++;//you are not allowed
here is also problem due to multiplication
double workingAverage = this.average * this.count;
seems like ok, can you prove it mathematically for more numbers as given in the problem statement
avg=2B
- Anonymous August 19, 2015