KatInterview

Find Nth Highest Distinct Salary

SQL & DataAuto-graded · 20 test cases
EasyAuto-gradedSQL & Dataarrayssortingsql

Last updated

Find the Nth highest unique value in a list of integers, returning None if N is out of valid bounds.

Given a list of integer compensation amounts salaries and a positive 1-indexed target rank n, determine the $n$-th highest distinct compensation amount.

If there are fewer than n distinct values in the input list, or if n is less than 1, return None.

Constraints

  • $0 \le \text{len}(salaries) \le 10^5$
  • $-10^9 \le salaries[i] \le 10^9$
  • $-10^9 \le n \le 10^9$

Examples

Example 1

Input: nth_highest_salary([70000, 90000, 80000], 1)

Output: 90000

Example 2

Input: nth_highest_salary([70000, 90000, 80000], 2)

Output: 80000

Example 3

Input: nth_highest_salary([70000, 90000, 80000], 4)

Output: None

Example 4

Input: nth_highest_salary([100, 200, 200, 300], 2)

Output: 200

Hints

Hint 1

Duplicate values should be collapsed so that ties share the exact same rank level.

Hint 2

Using a set or hash-based collection lets you isolate unique elements before sorting.

Hint 3

Be careful with non-positive values of n and 1-based indexing offsets.