Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
How many largest number do you wanna eliminate each interaction? 1/3 of the total largest numbers?
I would like to know why you miss 534 900 600? 534 is not event the x3 times 289?
This is Straight O(n) Algo.
1> For each element in a list
2> Sum up the values.
3> track the largest value observed so far.
4> end of Loop
5> Eliminate the largest value from the summation . Sum - largest.
6> Divide the sum by the (counter-1) from the loop.
1-4 is O(n) and the 5,6 is constant 2 steps overall O(n)
#include<iostream>
using namespace std;
int main()
{
double a[10000];
float average=0,sum=0;
int n,i;
cout<<"Enter The number of your digit plz \n";
cin>>n;
cout<<"The num of your digit is ="<<n;
cout<<"\n Enter your num now \n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
sum+=a[i];
average =sum/n;
cout<<"The average is ="<<average;
return 0;
}
cpp
#include <iostream>
#include <vector>
using namespace std;
void main()
{
vector <int> m_vec;
int m_in;
cout<<"Use control-z to stop"<<endl;
while(cin >> m_in )
{
m_vec.push_back(m_in);
}
vector<int>::size_type ix, i_max;
int m_max, m_min, m_remove;
m_remove=3;
for (int i=0; i!=m_remove; ++i)
{
m_max= m_vec[0];
i_max=0;
for (ix=1; ix!=m_vec.size(); ++ix)
{
if (m_vec[ix]> m_max)
{
m_max=m_vec[ix];
i_max=ix;
}
}
m_vec.erase(m_vec.begin()+i_max);
}
int m_avg=0;
double m_result=0;
for (ix=0; ix!=m_vec.size(); ++ix)
{
m_avg+=m_vec[ix];
}
m_result=(double)m_avg/m_vec.size();
cout<< m_result<< endl;
}
This one is O(n) to get the largest m numbers in an array of size n
public class AdviseredAverage
{
public static double getAdviseredAverage(int[] list, int n)
{
if (n >= list.length)
{
System.out.println(" Input Error: n must be < list.length");
return -1;
}
// Intialize elimination list
int[] eliminationList = new int[n];
// Initialize currentMin in elimination
int currentMin = list[0];
int currentMinIndex = 0;
// Initialize sum to 0
int sum = 0;
for (int i = 0; i < n; i++)
{
eliminationList[i] = list[i];
if (eliminationList[i] < currentMin)
{
// update minimum
currentMin = eliminationList[i];
currentMinIndex = i;
}
sum += list[i];
}
for (int i = n; i < list.length; i++)
{
if (list[i] > currentMin)
{
eliminationList[currentMinIndex] = list[i];
// update minimum
currentMin = eliminationList[0];
currentMinIndex = 0;
for (int j = 0; j < n; j++)
{
if (eliminationList[j] < currentMin)
{
// update minimum
currentMin = eliminationList[j];
currentMinIndex = j;
}
}
}
// add current number to sum
sum += list[i];
}
// subtract largest n numbers from sum
for (int i = 0; i < n; i++)
{
sum -= eliminationList[i];
}
return (double) (((double) sum) / ((double) (list.length - n)));
}
public static void main(String[] args)
{
int[] list = { 15,500,16,18,29,65,98,789,654,12,32,59,744,32,56,1254,12 };
System.out.println(getAdviseredAverage(list, 5));
}
}
- Anand J March 11, 2013