Nth-Highest Book Sales Volume
Updated
Problem statement
The book_sales table has one row per book: a unique book_id, the book's title, and its sales_volume, the number of copies sold. The query_params table has exactly one row, and its n column holds a positive integer N.
Return the Nth-highest distinct sales_volume. Books with the same sales volume share one position. For example, if the sales volumes are 700, 700 and 400, the 2nd-highest distinct value is 400.
Return one column named sales_volume with one row. If there are fewer than N distinct sales volumes, return no rows.
Interview follow-up
The interview built up to this question in steps:
- Find the highest sales volume.
- Find the Nth-highest sales volume.
- Find it again without
LIMITand without nested queries. The interviewer wanted a function here, which means a window function.
Try to answer with a window function. The judge checks only the result rows.
Table schema
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
book_sales
One row per book.
| Column | Type | Nullable | Description |
|---|---|---|---|
| book_idPK | Integer | No | — |
| title | Text | No | — |
| sales_volume | Integer | No | — |
query_params
Exactly one row, with param_id 1.
| Column | Type | Nullable | Description |
|---|---|---|---|
| param_idPK | Integer | No | — |
| n | Integer | No | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| sales_volume | Integer | No | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
- The
book_salestable has from0to10^5rows. book_idis unique, and1 <= book_id <= 10^9.titlehas 1 to 100 characters. Two books can have the same title.0 <= sales_volume <= 10^9.query_paramshas exactly one row, withparam_id = 1and1 <= n <= 10^5.- No column is
NULL.
More Amazon Phone Screen questions
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 3SumPHONE SCREEN · Seen Nov 2026
- Circular Primes Below a LimitPHONE SCREEN · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026
- Count the 1 Bits in a 32-Bit ValuePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Nth Highest SalaryPaytm · PHONE SCREENPaytm interview questions
- Email Funnel: Opened Within a Day, Replied Within a WeekSuperhuman · PHONE SCREENSuperhuman interview questions
- iPhone Model Sales Share by CountryApple · PHONE SCREENApple interview questions
- Top Three Accounts by Monthly Transaction AmountMastercard · PHONE SCREENMastercard interview questions
- In-Memory Database QueriesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Top Three Sales per County and YearClickhouse · PHONE SCREEN
- Block Type with the Most ChildrenNotion · PHONE SCREENNotion interview questions
- Third-Highest Known SalaryAdobe · PHONE SCREENAdobe interview questions
- Days Between Each User's Latest Two PostsZip · PHONE SCREENZip interview questions
- Observed-User Percentage by AppMicrosoft · PHONE SCREENMicrosoft interview questions