Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Top N Performers

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Find Top N Performers. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Pearson assessment results arrive as a list of student records. Given the records and an integer n, return the IDs of the top n performers without fully sorting a potentially large list.

A performer with a higher score ranks above one with a lower score. If scores are tied, preserve the students' original input order. Return the selected IDs ordered from highest score to lowest score, with tied students in their original order.

Formal Specification

Implement top_performers(students, n).

  • Input: students, a list of dictionaries with unique string student_id and integer score fields; n, a positive integer.
  • Output: A list of up to n student ID strings, ranked according to the rules above.

Use an approach that is more efficient than sorting the entire list when n is much smaller than the number of students.

Constraints

  • 0 <= len(students) <= 10^6
  • 1 <= n <= 10^5
  • 0 <= score <= 100
  • Student IDs are unique strings
  • If n exceeds the number of students, return all student IDs

Function Signature

def top_performers(students, n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output