Google Interview Question
Software Engineer / DevelopersCountry: United States
class Student
{
String id;
ArrayList<Double> scores;
Map<String, ArrayList<Double>> mapScores;
Map<String, Double> map;
public Student(String id)
{
this.id = id;
scores = new ArrayList<Double>();
mapScores = new HashMap<String, ArrayList<Double>>();
map = new HashMap<String, Double>();
}
public void addScore(double score)
{
scores.add(score);
setStudentScores();
}
public void setStudentScores()
{
mapScores.put(id, scores);
}
public Map<String, ArrayList<Double>> getStudentScores()
{
return mapScores;
}
public void averageScore()
{
//if(!mapScores.containsKey(id))
ArrayList<Double> scores = mapScores.get(id);
//System.out.println(scores);
Collections.sort(scores);
//System.out.println(scores);
double sum = 0;
for(int i = scores.size() - 1; i >= scores.size() - 5; i--)
sum += scores.get(i);
double average = sum / 5.0;
map.put(id, average);
}
public Map<String, Double> getAverageScore()
{
averageScore();
return map;
}
}
public class StudentScores
{
public static void main(String[] args)
{
Student student1 = new Student("1");
student1.addScore(80);
student1.addScore(100);
student1.addScore(90);
student1.addScore(70);
student1.addScore(50);
student1.addScore(60);
student1.addScore(85);
student1.addScore(95);
student1.addScore(82);
student1.addScore(78);
student1.addScore(88);
student1.addScore(96);
System.out.println(student1.getAverageScore().toString());
}
}
That sounds like homework, not an interview question..
- Miguel Oliveira October 09, 2013