KatInterview

Per-Group Top-3 Distinct Values with Tie Handling

SQL & DataAuto-graded · 17 test cases
MediumAuto-gradedSQL & Datagroupingdense-ranksorting

Last updated

Tests whether you can isolate the three highest distinct numeric values within each group and return all records that match those values, preserving correct sort order.

Per-Group Top-3 Distinct Values with Tie Handling

You are given a list of employee records. Each record is a dictionary with four fields:

  • id — unique integer identifier
  • name — employee name string
  • salary — integer compensation amount
  • dept_id — integer department identifier

Your task is to return every employee whose salary falls within the three highest distinct salary amounts in their department.

What "three highest distinct amounts" means

Count salary levels, not headcount. If two employees in the same department earn the same amount, that counts as one salary level, not two. So the three slots are filled by the three largest unique salary figures in the group.

  • If a salary level is tied by multiple employees, every one of them is included in the output.
  • If a department has fewer than three distinct salary amounts, all of its employees are returned.
  • The third-highest salary level is always included in full — no employee earning that amount is dropped.

Output format

Return a list of the same employee dictionaries (unchanged), sorted by:

  1. dept_id ascending
  2. salary descending

When two employees share the same dept_id and salary, preserve their original order from the input list (stable sort).

Constraints

  • 0 <= len(employees) <= 10^4
  • Each employee dict contains exactly the keys id, name, salary, dept_id.
  • 1 <= salary <= 10^9
  • dept_id is a positive integer.
  • Employee id values are unique.
  • An empty input list must return an empty list.

Examples

Example 1

Input: top_three_salaries_per_department([{'id': 1, 'name': 'Alice', 'salary': 120000, 'dept_id': 10}, {'id': 2, 'name': 'Bob', 'salary': 90000, 'dept_id': 10}, {'id': 3, 'name': 'Charlie', 'salary': 115000, 'dept_id': 20}, {'id': 4, 'name': 'David', 'salary': 130000, 'dept_id': 10}, {'id': 5, 'name': 'Eve', 'salary': 110000, 'dept_id': 20}])

Output: [{'id': 4, 'name': 'David', 'salary': 130000, 'dept_id': 10}, {'id': 1, 'name': 'Alice', 'salary': 120000, 'dept_id': 10}, {'id': 2, 'name': 'Bob', 'salary': 90000, 'dept_id': 10}, {'id': 3, 'name': 'Charlie', 'salary': 115000, 'dept_id': 20}, {'id': 5, 'name': 'Eve', 'salary': 110000, 'dept_id': 20}]

Hints

Hint 1

Start by partitioning the employee list into per-department buckets. Once you have a bucket, the problem reduces to finding the top-3 distinct values in a single list — what data structure makes deduplication trivial?

Hint 2

After collecting unique salaries for a department, sort them in descending order and slice off the first three. Store those three values in a set so membership checks stay O(1) when you scan the bucket for qualifying employees.

Hint 3

The final sort needs two keys: dept_id ascending and salary descending. A tuple key like (dept_id, -salary) handles both in one pass. Python's sort is stable, so ties in both keys automatically preserve input order.