← Questions
Top three scores for each student
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Apple's technical phone screen, last asked Aug 2025. It tests hash map, heaps and bucket sort.
You get a list of (student_id, score) records. Return, for each student, their three highest scores in descending order. First solve it with no assumptions about the scores. Then optimize for the case where every score is an integer from 0 to 100.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
top_three(records: list[tuple[str, int]]) -> dict[str, list[int]]: a student with fewer than three scores gets all of theirs. Equal scores count separately.
Example
records = [("ana", 88), ("bo", 70), ("ana", 95), ("ana", 88), ("ana", 60), ("bo", 99)]
top_three(records) -> {"ana": [95, 88, 88], "bo": [99, 70]}
Constraints
- Up to
10^6records and10^5students. - General case: scores are any integers. Optimized case: integers in
[0, 100].
Clarifying questions
- Should a repeated score count twice, or only distinct scores?
- What about students with fewer than three scores?
- Does the output need to be ordered by student?
- Do records arrive all at once, or as a stream?
What a strong answer covers
- Group by student, then keep a min-heap of size 3 per student: O(n log 3) = O(n) time, O(students) memory.
- Why sorting every group is O(n log n) and wasteful for a fixed
k. - With scores in 0 to 100: a count array of 101 per student, or a single pass keeping the top three in three variables, and the trade-off between the two.
- Tests: ties, fewer than three scores, one student, empty input.
Follow-ups
- Top
kinstead of three. - The records don't fit in memory: process them in a streaming or distributed way.
- Return the top three distinct scores.
Where it's asked
- Apple · Technical phone screen · Last asked Aug 2025
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
