Sollicitatievraag bij Goldman Sachs

Coderpad: given an array scores[][] = {“jerry”,”65”},{“bob”,”91”}, {“jerry”,”23”}, {“Eric”,”83”}} Find the student with highest average score

Antwoorden op sollicitatievragen

Anoniem

6 dec 2018

import java.util.*; import java.util.stream.Collectors; class Student{ private String name; private Float averageMark = 0.0f; private int numberOfSubjects = 0; public Student(String name){ this.name = name; } public void addMark(Float mark ){ averageMark += mark; numberOfSubjects += 1; averageMark = averageMark/numberOfSubjects; } public Float getAverageMark(){ return averageMark; } public String getName(){ return name; } public Student(Float mark, String n){ name = n; averageMark = mark; } } public class MaxScore { public static String s[][] = {{"jerry","65"}, {"bob","91"}, {"jerry","23"}, {"Eric","83"},{"Eric","99"}}; public static void main(String args[]){ Map studentMap = new HashMap(); for(int i=0; i studentList = studentMap.values().stream().collect(Collectors.toList()); Collections.sort(studentList, Comparator.comparing(Student::getAverageMark).reversed()); System.out.println(studentList.get(0).getName()); } }

6

Anoniem

5 nov 2019

package Hello; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import static java.util.Comparator.comparingInt; public class hello { public static class Average { public int count; public int num; public int average; public Average(int count, int num, int average) { super(); this.count = count; this.num = num; this.average = average; } public Average() { super(); } } public static void main(String[] args) { String s[][] = {{"jerry","65"}, {"bob","91"}, {"jerry","23"}, {"Eric","83"}, {"bob","10"}}; Map map = new HashMap(); int avera = 0; try { for(String x[]:s) { if(map.containsKey(x[0])) { Average avg = map.get(x[0]); int val = avg.num + Integer.parseInt(x[1]); int count = ++avg.count; int average = val/count; map.put(x[0], new Average(count, val , average)); } else { if(x[0] != null) { int val = Integer.parseInt(x[1]); map.put(x[0], new Average(1, val, val )); } } } avera = map.entrySet() .stream() .max(comparingInt(e -> e.getValue().average)).get().getValue().average; } catch(Exception e) { } System.out.println(avera); } }

2

Anoniem

3 jun 2020

We can use java-8 streams API to get the student with maximum marks easily in 2 line of codes. String [][]scores = {{"jerry","65"},{"bob","91"}, {"jerry","23"}, {"Eric","83"}} ; Map collect = Arrays.stream(scores).collect(Collectors.groupingBy(a -> a[0], Collectors.averagingInt(a->Integer.parseInt(a[1])))); Entry studentWithMaxMarks = collect.entrySet().stream().max((e1,e2) -> Double.compare(e1.getValue(), e2.getValue())).get(); System.out.println( studentWithMaxMarks.getKey() +" : "+studentWithMaxMarks.getValue() );

1

Anoniem

25 jun 2020

package com.company; import java.util.*; public class BestGrade { public static void main(String[] args) { String student[][] = {{"jerry","65"}, {"bob","60"}, {"jerry","42"},{"jerry","65"}, {"jerry","100"},{"Eric","63"}}; Map> map = new HashMap(); for(int i = 0; i templst = new ArrayList(student.length); if (map.containsKey(student[i][0])) { templst.clear(); templst = map.get(student[i][0]); templst.add(Integer.parseInt(student[i][1])); } else { templst.clear(); templst.add(Integer.parseInt(student[i][1])); } map.put(student[i][0],templst); } String highestAverageStudent = ""; double highestAverage = 0; for(String key : map.keySet()) { double average = map.get(key).stream().mapToInt(val -> val).average().orElse(0.0); if (highestAverage < average) { highestAverage = average; highestAverageStudent = key; } } System.out.println(highestAverageStudent); } }

Anoniem

29 sep 2020

/** Input: [{"Bob","87"}, {"Mike", "35"},{"Bob", "52"}, {"Jason","35"}, {"Mike", "55"}, {"Jessica", "99"}] Input [["Bob","87"], ["Mike", "35"], ["Bob", "52"], ["Jason","35"], ["Mike", "55"], ["Jessica", "99"]] Output: 99 Explanation: Since Jessica's average is greater than Bob's, Mike's and Jason's average. */ const calculateAvgGrade = (arr) => { if(arr.length == 0) { return; } // O(K) Space Complex where K is number of unique Keys const hashMap = new Map(); let highest = -Infinity; // O(N) Time Complexity - where N is arr length arr.forEach((element) => { if(!hashMap.has(element[0])) { hashMap.set(element[0], {count: 0, totalScore: 0 }); } let temp = hashMap.get(element[0]); temp.count++; temp.totalScore = temp.totalScore + Number(element[1]); hashMap.set(element[0], temp); }); hashMap.forEach((value, key) => { let temp = Math.floor(value.totalScore/value.count); if(temp > highest) { highest = temp; } }); return highest; };

Anoniem

4 okt 2020

li = [["jerry",65],["bob",91], ["jerry",23], ["Eric",83]] def get_student_with_highest_avg(li): stu_dict = {} for stu in li: stu_name = stu[0] stu_marks = stu[1] if(stu_name in stu_dict): stu_prev_avg = stu_dict[stu_name] stu_avg = (stu_marks + stu_prev_avg)/2 stu_dict[stu_name] = stu_avg else: stu_dict[stu_name] = stu_marks max_avg = 0 student_name_with_highest_avg = '' for item in stu_dict.iteritems(): if(item[1] > max_avg): max_avg = item[1] student_name_with_highest_avg = item[0] return student_name_with_highest_avg, max_avg print(get_student_with_highest_avg(li))

Anoniem

5 mei 2020

package com.rsb.hack; import java.util.ArrayList; import java.util.List; public class Maxsumarray2D { public static void main(String[] args) { String[][] str = { { "Bob", "80" }, { "Rob", "70" }, { "Charles", "85" }, { "Bob", "100" }, { "Charles", "75" } }; int sum = 0; List ls = new ArrayList(); for (int row = 0; row < str.length; row++) { for (int col = 0; col < str[row].length; col++) { if(col!=0) { sum+= Integer.parseInt(str[row][col]); } } } System.out.println(sum); Double avrage=(double) (sum/str[0].length); System.out.println(avrage); } }

Anoniem

13 mei 2020

public static void avrage() { String[][] str = { { "Bob", "80" }, { "Rob", "100" }, { "Charles", "50" }, { "Bob", "80" }, { "Charles", "50" } }; int sum = 0; Map map = new HashMap(); for (int row = 0; row < str.length; row++) { for (int col = 0; col < str[row].length; col++) { if (col == 0) { name=str[row][col]; } if (col != 0) { b= Integer.parseInt(str[row][col]); } if(map.containsKey(name)) { int sum1=map.get(name); int total=sum1+b/2; map.put(name, total); total=0; b=0; } else { map.put(name, b); } } } System.out.println("map value-------"+map); }

Anoniem

13 mei 2020

package com.rsb.hack; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Maxsumarray2D { static String name = ""; static int b = 0; public static void main(String[] args) { avrage(); } public static void avrage() { String[][] str = { { "Bob", "80" }, { "Rob", "100" }, { "Charles", "50" }, { "Bob", "80" }, { "Charles", "50" } }; int sum = 0; Map map = new HashMap(); for (int row = 0; row la:map.entrySet()) { if(la.getValue()>avgmax) { avgmax=la.getValue(); } } System.out.println("avgmax----"+avgmax); } }

Anoniem

9 okt 2018

public class BestGrade { public static void main(String[] args) { String student[][] = {{"jerry","65"}, {"bob","1"}, {"jerry","23"},{"jerry","23"}, {"jerry","100"},{"Eric","83"}}; //Map counter = new HashMap(); List score = new ArrayList(); Map> map = new HashMap(); for(int i = 0;i(); score.add(Integer.parseInt(student[i][1])); map.put(student[i][0], score); } } List grade = new ArrayList(); for(String key:map.keySet()) { score = map.get(key); int sum = 0; for(int num : score) { sum+=num; } int average = sum/score.size(); grade.add(average); } Collections.sort(grade); System.out.println(grade.get(grade.size()-1)); } }

1

Anoniem

29 mei 2020

import java.util.HashMap; import java.util.Map; public class BestAverageGrade { public static void main(String[] args) { // TODO Auto-generated method stub String s[][] = {{"jerry","65"}, {"bob","91"}, {"jerry","23"}, {"Eric","83"}}; HashMap hm = new HashMap(); for(int i=0;i entry : hm.entrySet()) { System.out.println(entry.getKey()+" " + entry.getValue()); } double average = Double.MIN_VALUE; String name=""; for(Map.Entry entry : hm.entrySet()) { if(entry.getValue() > average) { name = entry.getKey(); average = entry.getValue(); } } System.out.println(name+" "+average); } }

Anoniem

12 jul 2020

public class HighestMarks { //Coderpad: given an array scores [][] = {“jerry”,”65”},{“bob”,”91”}, {“jerry”,”23”}, {“Eric”,”83”}} //Find the student with highest average score public static String s[][] = {{"jerry","65"}, {"bob","91"}, {"jerry","23"}, {"Eric","83"}}; public static void main (String args[]) { int highestScore = 0; int studentIndex = 0; int temp = 0; // s.length should returns no of row for(int row = 0; row highestScore) { highestScore = marks; studentIndex = row; } } System.out.print("Student with highest marks:"+s[studentIndex][0]); } }

Anoniem

14 nov 2019

Can anybody please tell, If anything is wrong with this simple approach : public class StudentWithMax { private static class Student { public String name; public Double avg; Student(String n, Double a) { name = n; avg = a; } } public static void main(String[] args) { String[][] s = { { "Jerry", "65" }, { "Bob", "92" }, { "Jerry", "33" }, { "Eric", "83" }, }; Student maxStudent= new Student("", (double)Integer.MIN_VALUE); for (String[] strings : s) { //System.out.println(strings[0]); if(Double.parseDouble(strings[1]) > maxStudent.avg) { maxStudent.name=strings[0]; maxStudent.avg=Double.parseDouble(strings[1]); } } System.out.println("name: "+maxStudent.name + ", avg: " + maxStudent.avg); } }

Anoniem

21 apr 2018

public static String s[][] = {{"jerry","65"}, {"bob","91"}, {"jerry","23"}, {"Eric","83"}}; public static void main (String args[]) { int highestScore = 0; int studentIndex = 0; int temp = 0; for(int i = 0; i highestScore){ highestScore = temp; studentIndex = i; } } System.out.println(s[studentIndex][0]);

14

Anoniem

13 jan 2019

you can have a hashmap with key string and arraylist of size2 where u can store the current sum in 0 index and num of scores in 1 index public static void main(String[] args) { String student[][] = {{"jerry","65"}, {"bob","1"}, {"jerry","23"},{"jerry","23"}, {"jerry","100"},{"Eric","83"}}; Map student> = new HashMap(); float max_avg=0.0; String key; for(int i = 0;i(); Integer score = Integer.parseInt(student[i][1])); if(map.containsKey(student[i][0]) { Integer sum = map.get(student[i][0]).get(0); Integer num = map.get(student[i][0]).get(1)+1; sum=sum+score; map.get(student[i][0]).set(0,sum); map.get(student[i][0]).set(1,num); avg =sum/score; if(avg>max_avg) { max_avg=avg; key=student[i][0]; } else { List ar = new ArrayList(); ar.add(score); ar.add(1); map.put(student[i][0], ar); } } System.out.println(map.get(key)); } O(n) solution single pass

2

Anoniem

23 apr 2019

public static Integer bestAverageGrade(String[][] scores) { List s = new ArrayList(); List grade = new ArrayList(); Map> map = new HashMap>(); for(int i = 0;i

1

Anoniem

23 apr 2018

Add all elements to the Hashmap , if key matches append the score to the list, now iterate through keys and keep track of avg and key, return max avg and key in the end O(N)

3

Anoniem

22 jan 2020

Solving same problem using Java 8: import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; public class MaxScore { public static String s[][] = {{"jerry","65"}, {"bob","91"}, {"jerry","23"}, {"Eric","83"}}; public static void main(String[] args) { List arrayOfLists = Arrays.asList(s); List students = arrayOfLists.stream().map(s->new Student(s)).collect(Collectors.toList()); Optional studentWithMaxScore = students.stream().max(Comparator.comparing(Student::getScore)); System.out.println(studentWithMaxScore.get().getScore()); } } class Student{ private final String name; private final int score; public Student(String[] s) { String name = s[0]; String score = s[1]; this.name = name; if(score.matches("-?\\d+(\\.\\d+)?")) { this.score = Integer.parseInt(score); } else { this.score = 0; } } public String getName() { return name; } public int getScore() { return score; } }

1

Anoniem

23 apr 2019

public static Integer bestAverageGrade(String[][] scores) { List s = new ArrayList(); List grade = new ArrayList(); Map> map = new HashMap>(); for(int i = 0;i

1