KatInterview

Best-Scoring Enrollee for Each Academic Department

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datagroup-wise selectionleft jointie-breaking

Last updated

Pick one top student per department from two record lists, using GPA with deterministic tie-breaks, skipping missing values, and keeping departments that have no eligible student.

You are given two lists of records that mimic a small university database. Implement the logic that a "top row per group" SQL query with an outer join would perform, but in Python.

Input

  • departments: a list of dictionaries, each with the keys dept_id (int) and dept_name (str). Each dept_id is unique.
  • students: a list of dictionaries, each with the keys:
    • student_id (int, unique)
    • student_name (str)
    • dept_id (int or None)
    • gpa (float or None)
    • enrolled_at (str, date in YYYY-MM-DD format)

Task

For every department, find its single best student and report it.

A student is eligible only if both dept_id and gpa are not None. Ineligible students are ignored completely. A student whose dept_id matches no department is also never reported.

Among the eligible students of one department, the best student is chosen by this order:

  1. Highest gpa.
  2. If GPAs are equal, the earliest enrolled_at (compare the date strings; the YYYY-MM-DD format sorts chronologically). A missing enrolled_at (None) is treated as an empty string, so it counts as earliest.
  3. If still tied, the smallest student_id.

Output

Return a list of dictionaries, one per department, in the same order as the input departments list. Each dictionary has exactly these keys:

dept_id, dept_name, student_id, student_name, gpa, enrolled_at

  • For a department with an eligible best student, the student fields are copied from that student.
  • For a department with no eligible student, return a row with student_id, student_name, gpa and enrolled_at all set to None.

The dept_id and dept_name always come from the department record.

Constraints

  • 0 <= len(departments), len(students) <= 10^5
  • dept_id values in departments are unique; student_id values are unique.
  • gpa is None or a number; dept_id of a student may be None.
  • enrolled_at strings use the YYYY-MM-DD format (or are None).
  • The output must contain exactly one row per input department, in input order.

Examples

Example 1

Input: best_student_by_department([{'dept_id': 1, 'dept_name': 'CS'}, {'dept_id': 2, 'dept_name': 'Math'}, {'dept_id': 3, 'dept_name': 'Physics'}, {'dept_id': 4, 'dept_name': 'History'}], [{'student_id': 101, 'student_name': 'Alice', 'dept_id': 1, 'gpa': 3.9, 'enrolled_at': '2022-09-01'}, {'student_id': 102, 'student_name': 'Bob', 'dept_id': 1, 'gpa': 3.9, 'enrolled_at': '2023-01-15'}, {'student_id': 103, 'student_name': 'Cara', 'dept_id': 2, 'gpa': 3.85, 'enrolled_at': '2021-09-01'}, {'student_id': 104, 'student_name': 'Dan', 'dept_id': 2, 'gpa': None, 'enrolled_at': '2024-02-01'}, {'student_id': 105, 'student_name': 'Eve', 'dept_id': None, 'gpa': 3.7, 'enrolled_at': '2023-09-01'}, {'student_id': 106, 'student_name': 'Frank', 'dept_id': 3, 'gpa': 3.9, 'enrolled_at': '2020-09-01'}])

Output: [{'dept_id': 1, 'dept_name': 'CS', 'student_id': 101, 'student_name': 'Alice', 'gpa': 3.9, 'enrolled_at': '2022-09-01'}, {'dept_id': 2, 'dept_name': 'Math', 'student_id': 103, 'student_name': 'Cara', 'gpa': 3.85, 'enrolled_at': '2021-09-01'}, {'dept_id': 3, 'dept_name': 'Physics', 'student_id': 106, 'student_name': 'Frank', 'gpa': 3.9, 'enrolled_at': '2020-09-01'}, {'dept_id': 4, 'dept_name': 'History', 'student_id': None, 'student_name': None, 'gpa': None, 'enrolled_at': None}]

Hints

Hint 1

Students with a missing GPA or missing department can never win, so discard them before comparing anything.

Hint 2

You do not need to sort everything: scan the students once and keep, per department id, the best candidate seen so far using a comparison key.

Hint 3

Build the key as (negated gpa, enrolled_at or empty string, student_id) so that a smaller key means a better student; then walk the departments list and look up each id, producing None fields when there is no entry.