KatInterview

Top Earners per Department with Ties

SQL & DataAuto-graded · 17 test cases
MediumAuto-gradedSQL & Datagroup-byhash-mapties

Last 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 employees list.
  • If employees is empty, return an empty list.

Constraints

  • 0 <= len(departments) <= 10^4
  • 0 <= len(employees) <= 10^5
  • department_id values in departments are unique, and so are employee_id values in employees.
  • Salaries are numbers and may be zero or negative.
  • Every department_id used in employees exists in departments.

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.