KatInterview

Current Top Earner in Each Department

SQL & DataAuto-graded · 15 test cases
EasyAuto-gradedSQL & Datagroupinghash-mapdata-manipulation

Last updated

Reduce salary history to each person's current pay per department, then choose the highest-paid person in each department, with exact tie and ordering rules.

Current Top Earner in Each Department

You are given pay records for a company. Each record is a dictionary describing one person's salary in one department. Return the best-paid person in every department, using only each person's current salary there.

Record format

Each record has these keys:

  • employee_id (int)
  • employee_name (str)
  • department_id (int)
  • department_name (str)
  • salary (number)
  • effective_date (str, optional): an ISO date such as "2024-01-01". The key may be absent.

A person can belong to several departments. A person can also have several records for the same department, for example after a raise or a cut.

Task

Implement highest_paid_per_department(records). It returns one dictionary per department, each describing that department's top earner.

Rules

  1. Current salary. Treat each (employee_id, department_id) pair separately. Among the records of a pair, the current one is the record with the latest effective_date.
    • Dates are compared as plain strings, which works because they are ISO formatted.
    • A record with no effective_date counts as older than every record that has one.
    • If several records of a pair tie for the latest date, or none of them has a date, the one that appears earliest in the input is current.
  2. Top earner. Within each department, consider only the current records. The top earner is the one with the highest salary.
    • If salaries tie, the winner is the pair whose first record appears earliest in the input list. Where that pair's current record appears does not matter.
    • Older records never take part in the comparison. A person whose pay was cut is judged on the reduced pay.
  3. Output. Return a list with exactly one dictionary per department, containing the keys department_id, department_name, employee_id, employee_name and salary. All values come from the winner's current record.
  4. Order. Departments appear in the order of their first appearance in the input.
  5. If records is empty, return an empty list.

Constraints

  • 0 <= len(records) <= 10^5
  • Salaries are non-negative numbers.
  • Every record has all keys except possibly effective_date.
  • A single department_id is always used with a consistent department name, but group by the id.

Examples

Example 1

Input: highest_paid_per_department([{'employee_id': 1, 'employee_name': 'Ava', 'department_id': 10, 'department_name': 'Engineering', 'salary': 150000, 'effective_date': '2024-01-01'}, {'employee_id': 2, 'employee_name': 'Ben', 'department_id': 10, 'department_name': 'Engineering', 'salary': 160000, 'effective_date': '2024-01-01'}, {'employee_id': 1, 'employee_name': 'Ava', 'department_id': 20, 'department_name': 'Product', 'salary': 155000, 'effective_date': '2024-01-01'}])

Output: [{'department_id': 10, 'department_name': 'Engineering', 'employee_id': 2, 'employee_name': 'Ben', 'salary': 160000}, {'department_id': 20, 'department_name': 'Product', 'employee_id': 1, 'employee_name': 'Ava', 'salary': 155000}]

Example 2

Input: highest_paid_per_department([{'employee_id': 1, 'employee_name': 'Ava', 'department_id': 10, 'department_name': 'Engineering', 'salary': 140000, 'effective_date': '2023-01-01'}, {'employee_id': 1, 'employee_name': 'Ava', 'department_id': 10, 'department_name': 'Engineering', 'salary': 130000, 'effective_date': '2024-01-01'}, {'employee_id': 2, 'employee_name': 'Ben', 'department_id': 10, 'department_name': 'Engineering', 'salary': 135000, 'effective_date': '2024-01-01'}])

Output: [{'department_id': 10, 'department_name': 'Engineering', 'employee_id': 2, 'employee_name': 'Ben', 'salary': 135000}]

Hints

Hint 1

Handle it in two passes: first reduce each (employee_id, department_id) pair to its current record, then pick a winner per department.

Hint 2

Replace a stored record only when the new one is strictly better (strictly later date, or strictly higher salary). A dictionary keeps the insertion order of its keys, which gives the required tie-breaks and the output order.

Hint 3

Use an empty string as the date when effective_date is missing, so any real ISO date compares as later.