Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Find Top N Performers
00:00
5 left

Find Top N Performers

MediumPython

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):
Interviewer

Your question is Find Top N Performers. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.