KatInterview

Clean, Impute and Rank Student Score Records

Heaps, Sorting & SearchAuto-graded · 15 test cases
EasyAuto-gradedHeaps, Sorting & Searchsortingmediandata-cleaning

Last updated

Filter records with too many missing values, fill gaps with per-subject medians, then pick the top five using a multi-key sort with tie-breaks.

You have a list of student records. Each record is a dictionary with these keys:

  • student_id: an integer
  • math_score: a number, or None if missing
  • english_score: a number, or None if missing
  • physics_score: a number, or None if missing

Write a function that cleans the records and returns the five best students. Follow these steps in this order:

  1. Discard sparse records. Remove every student who has None in two or more of the three subjects. A student with exactly one missing subject stays.
  2. Fill the gaps. For each subject, compute the median of that subject's non-missing values among the students who survived step 1. Replace each remaining None in that subject with this median.
    • For an odd count of values, the median is the middle value of the sorted list.
    • For an even count, it is the average of the two middle values.
  3. Rank. Sort the cleaned students by:
    1. math_score, highest first;
    2. then physics_score, highest first;
    3. then student_id, lowest first.
  4. Select. Keep the first 5 students after sorting. If fewer than 5 remain, keep all of them.

Return the selected students as a list of dictionaries in ranked order. Each dictionary holds the full cleaned record with all four keys, so filled-in values appear in place of None. The input must not be modified.

Constraints

  • 0 <= len(students) <= 10^5
  • Every record has all four keys. Missing scores are None.
  • Medians are computed only after the sparse records are removed. Medians over the original list are not used.
  • After step 2, every student has a numeric math_score and physics_score. A subject whose values are all missing among the survivors does not occur in the sort keys.
  • Duplicate student_id values are not merged or removed.
  • If no students survive step 1, return an empty list.

Examples

Example 1

Input: rank_top_students([{'student_id': 1, 'math_score': 90, 'english_score': 80, 'physics_score': 85}, {'student_id': 2, 'math_score': None, 'english_score': 70, 'physics_score': 95}, {'student_id': 3, 'math_score': 75, 'english_score': None, 'physics_score': None}, {'student_id': 4, 'math_score': None, 'english_score': None, 'physics_score': 60}])

Output: [{'student_id': 2, 'math_score': 90, 'english_score': 70, 'physics_score': 95}, {'student_id': 1, 'math_score': 90, 'english_score': 80, 'physics_score': 85}]

Hints

Hint 1

Count the None values per record first and drop records with two or more. Do this before computing any statistic.

Hint 2

Compute each subject's median only from the surviving records, ignoring None. Sort the non-missing values and take the middle, averaging the two middle values when the count is even.

Hint 3

Copy the records before filling gaps so the input is untouched. Use a sort key of (-math_score, -physics_score, student_id), then slice the first 5.