Top Earners per Department with Ties
SQL & DataAuto-graded · 17 test casesLast updated
Given department and employee tables as lists, return every employee who earns the maximum salary in their department, keeping ties and handling duplicate names.
Top Earners per Department
A company's data is stored as two tables, passed to your function as lists of rows.
departments: each row is[department_id, name].employees: each row is[employee_id, name, salary, department_id].
Every department_id in employees refers to a row in departments.
Write a function that returns every employee whose salary equals the highest salary within that employee's own department.
Output
Return a list of rows, each of the form [department_name, employee_name, salary].
- If several employees in a department share the top salary, include all of them.
- A department with no employees produces no rows.
- Department names are not guaranteed to be unique. Two departments with different ids but the same name are separate departments, and each is evaluated on its own using
department_id. The output may therefore contain the same department name for different departments. - Employee names are not guaranteed to be unique. Each employee row is judged independently, so two employees with identical names can both appear.
- Output rows must follow the order in which the qualifying employees appear in the input
employeeslist. - If
employeesis empty, return an empty list.
Constraints
0 <= len(departments) <= 10^40 <= len(employees) <= 10^5department_idvalues indepartmentsare unique, and so areemployee_idvalues inemployees.- Salaries are numbers and may be zero or negative.
- Every
department_idused inemployeesexists indepartments.
Examples
Example 1
Input: top_earners_by_department([[1, 'Engineering'], [2, 'Sales'], [3, 'HR']], [[101, 'Alice', 120000, 1], [102, 'Bob', 135000, 1], [103, 'Carol', 90000, 2], [104, 'Dan', 90000, 2], [105, 'Eve', 70000, 3]])
Output: [['Engineering', 'Bob', 135000], ['Sales', 'Carol', 90000], ['Sales', 'Dan', 90000], ['HR', 'Eve', 70000]]
Example 2
Input: top_earners_by_department([[10, 'Analytics'], [20, 'Analytics'], [30, 'Product']], [[1, 'Sam', 100, 10], [2, 'Sam', 100, 10], [3, 'Lee', 200, 20], [4, 'Pat', 150, 30]])
Output: [['Analytics', 'Sam', 100], ['Analytics', 'Sam', 100], ['Analytics', 'Lee', 200], ['Product', 'Pat', 150]]
Example 3
Input: top_earners_by_department([[1, 'Empty'], [2, 'Finance']], [[11, 'Nora', 50000, 2]])
Output: [['Finance', 'Nora', 50000]]
Hints
Hint 1
For each employee you only need to know one thing: does their salary equal the maximum salary of their department?
Hint 2
Compute the maximum salary per department in one pass over the employees, keyed by department_id and not by name. Seed each maximum from a real salary so negative values work.
Hint 3
In a second pass over the employees in input order, keep rows whose salary equals their department's maximum, and look up the department name by id only when building the output row.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The question for each employee is whether their salary equals the maximum salary in their department. This splits into two passes.
- Walk through all employees and record the largest salary seen for each
department_id. The first employee seen in a department seeds its maximum, so negative or zero salaries are handled correctly. - Walk through the employees again in input order. Keep every employee whose salary equals the stored maximum for their department, and build the output row
[department_name, employee_name, salary]using a lookup fromdepartment_idto name.
Why it is correct
- Sound: a row is emitted only when its salary equals the true maximum of its department, because the first pass sees every employee of that department.
- Complete: every employee whose salary equals the maximum passes the equality test, so ties are never dropped. Using equality against the maximum, rather than picking a single top row, is what makes this work.
- Identity by id: all grouping uses
department_id, so two departments sharing a name are never merged. The name is only read when building output. - Empty departments: output is driven only by employees, so a department with none never appears.
- Order: the second pass follows input order, so output order is deterministic.
Complexity
With E employees and D departments, time is O(D + E), since dictionary operations are O(1) on average. Space is O(D) for the two dictionaries plus O(k) for the k output rows.
Reference solution
from typing import List
def top_earners_by_department(departments: List[List], employees: List[List]) -> List[List]:
# Map department id -> display name (names may repeat, ids do not)
names_by_id = {dept_id: dept_name for dept_id, dept_name in departments}
# First pass: maximum salary for each department id
max_salary = {}
for emp_id, emp_name, salary, dept_id in employees:
if dept_id not in max_salary or salary > max_salary[dept_id]:
max_salary[dept_id] = salary
# Second pass: keep everyone who matches their department's maximum
result = []
for emp_id, emp_name, salary, dept_id in employees:
if salary == max_salary[dept_id]:
result.append([names_by_id[dept_id], emp_name, salary])
return result