FastPrepBest Average Student Score

Best Average Student Score

Goldman Sachs logoGoldman Sachs● EasyFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Each row of records contains a student name followed by one signed integer score written as a decimal string. A student may appear in multiple rows.

Return the name of the student with the greatest arithmetic mean across that student's scores. If several students have exactly the same mean, return the lexicographically smallest name.

Compare averages exactly; do not rely on floating-point rounding.

Function

bestAverageStudent(records: String[][]) → String

Examples

Example 1

records = [["Alice","90"],["Bob","80"],["Alice","70"],["Bob","100"]]return = "Bob"

Alice averages 80 and Bob averages 90.

Example 2

records = [["Zoe","10"],["Amy","10"]]return = "Amy"

The averages tie, so the lexicographically smaller name wins.

Constraints

  • 1 <= records.length <= 200000
  • Every row contains exactly two strings: a nonempty name and a valid 32-bit signed integer score.
  • The total score for one student fits in a signed 64-bit integer.

More Goldman Sachs problems

See Goldman Sachs hiring insights
public String bestAverageStudent(String[][] records) {
  // write your code here
}
records[["Alice","90"],["Bob","80"],["Alice","70"],["Bob","100"]]
expected"Bob"
Checking account…