Find the Top Student

Given an integer n representing the number of students and a list of n students. Each student is represented by a Student structure containing the following fields:

  • name: A string representing the student's name.
  • marks: A double representing the marks obtained by the student.

Your task is to find and return the student with the highest marks.

If multiple students have the same highest marks, return the student who appears first in the given list.

Return the complete student record, including the student's name and marks. The list will always contain at least one student.

Note: Each student is provided as a list of name and marks in the input The drive coder converts the input list into a vector<Student> before calling findTopStudent().

Example 1:

Input: n = 3, students = [ {name: "Alice", marks: 85.5}, {name: "Bob", marks: 92.0}, {name: "Charlie", marks: 88.0}]

Output: ["Bob", 92.0]

Explanation: Alice has 85.5 marks, Bob has 92.0, and Charlie has 88.0. The highest marks are 92.0 at index 1, where the name is Bob. Return Bob's complete record, ["Bob", 92.0].

Example 2:

Input: n = 3, students = [ {name: "Alice", marks: 95.0}, {name: "Bob", marks: 95.0}, {name: "Charlie", marks: 90.0}]

Output: ["Alice", 95.0]

Explanation: Alice and Bob both have the highest marks, 95.0. Alice appears at index 0, before Bob at index 1. The tie is resolved by original array order, so return ["Alice", 95.0].

Now Your Turn!

Pick the correct output for the given input

Input: n = 1,students = [{name: "David", marks: 76.5} ]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 104
  • Both arrays contain n elements.
  • Each name contains 1 to 50 English letters, with no spaces.
  • 0 <= marks[i] <= 100
  • Marks have at most six decimal places.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
* Definition for a student.
* struct Student {
* std::string name;
* double marks;
* };
*/
class Solution {
public:
Student findTopStudent(const std::vector<Student>& students) {
// Write your code here
}
};
 
Test Case

Input:

Marks
N
Names