medium +20 pts

Nth Highest Salary

Query the Nth highest distinct salary from an employee table using SQLite.

You are given a SQLite table named `employee` with the following schema: ```sql CREATE TABLE employee ( id INTEGER PRIMARY KEY, salary INTEGER ); ``` Write a Python function `nth_highest_salary(N: int)` that connects to an in-memory SQLite database, creates the `employee` table, inserts the sample data (provided in the test harness), and returns the Nth highest **distinct** salary. If there are fewer than N distinct salaries, return `None`. For N <= 0, return `None`. The function must use SQL to compute the result. The test harness will create the database, create the table, insert the sample data, and then call your function with a fresh connection to the same in-memory database. However, to be self-contained and pass the tests, your function should create its own in-memory database, create the table, and insert the sample data that the test harness provides. The test harness will insert the sample data before calling your function, so you should NOT insert additional data. The sample data is provided in the test cases (see Examples). The function should only query and return the result. **Important**: Your function must handle the insertion of sample data by the test harness. The test harness will create the table and insert the data before invoking your function, using the same connection? Actually, each test case will call your function with a fresh in-memory database, and your function is responsible for setting up the table and inserting the sample data. The sample data is given in the Examples. Your function should read from the table that it itself creates and populates. The exact sample data for each test case is specified in the test cases below, but to keep the function deterministic, assume the table always contains the salaries [100, 200, 300, 200, 100] (as described in Examples). Your function should work for any data that the test harness might insert, but for grading, the test harness will insert that exact data. To avoid ambiguity: your function should create the database, create the table, insert the sample data [100, 200, 300, 200, 100] (with auto-generated ids), and then run the query. This way the function is self-contained and predictable for the given examples. The test harness will call your function with only the parameter N, not with any data input. Therefore, your function must hardcode the sample data. This is acceptable for this problem. **Function signature**: `def nth_highest_salary(N: int)` returns an `int` or `None`.

Constraints

The table always contains exactly 5 rows with salaries [100, 200, 300, 200, 100] for the given examples. In general, constraints: 1 <= number of rows <= 10^4, 0 <= salary <= 10^9. N can be any integer (including negative or zero). Expected time complexity: O(n log n) due to sorting, but SQL optimization is acceptable.

Example

```python
>>> nth_highest_salary(1)
300
>>> nth_highest_salary(2)
200
>>> nth_highest_salary(3)
100
>>> nth_highest_salary(4)
None
>>> nth_highest_salary(0)
None
>>> nth_highest_salary(-1)
None
```
20 points ~20 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `SELECT DISTINCT salary FROM employee ORDER BY salary DESC LIMIT 1 OFFSET ?`.
Handle the case when N <= 0 by returning None early.
If the query returns no row, return None.
Remember to close the connection after fetching the result.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.