All challenges
Medium 0 solved 0% acceptance

Gradebook Top Student

Find the student with the highest average score.

What you'll learn

  • Average each row of scores
  • Find the best student
  • Break ties consistently

Problem

Each row of scores holds one student's test scores. Compute each student's average with a helper function and return the row index of the student with the highest average. If several tie, return the smallest index.

Implement top_student(scores).

Input

scores: One row of scores per student; every row has at least one score.

Output

Return the index of the top student.

Constraints

1 ≤ students ≤ 1,000; 1 ≤ scores per student ≤ 100; 0 ≤ score ≤ 100

Examples

Example 1
Input:
scores = [
    [80, 90],
    [95, 85],
    [70, 100]
]

Expected output:
expected = 1
Input:
std::vector<std::vector<long long>> scores = {
    {80, 90},
    {95, 85},
    {70, 100}
};

Expected output:
long long expected = 1;
Input:
long[][] scores = new long[][] {
    new long[] {80, 90},
    new long[] {95, 85},
    new long[] {70, 100}
};

Expected output:
long expected = 1;

Explanation

Averages are 85, 90 and 85, so student 1 has the highest average.

Example 2
Input:
scores = [
    [60],
    [60, 60]
]

Expected output:
expected = 0
Input:
std::vector<std::vector<long long>> scores = {
    {60},
    {60, 60}
};

Expected output:
long long expected = 0;
Input:
long[][] scores = new long[][] {
    new long[] {60},
    new long[] {60, 60}
};

Expected output:
long expected = 0;

Explanation

Both averages are 60; the tie goes to the smaller index, 0.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Compare students by their average score, not by a single test.

Hint 2

Compute each row’s average and keep the best index seen so far.

Hint 3

When averages tie, keep the earlier student instead of replacing it.

Expected complexity

O(total scores) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.