Top Three Products per Category
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Table products:
id INTEGER primary key
name TEXT
category TEXT
Table sales:
product_id INTEGER
quantity INTEGER
unit_price REAL
A product's revenue is the sum of quantity × unit_price over its sales. A product with no sales has revenue 0.
Return the products whose revenue is among the three highest distinct revenues in their category. Ties share a rank, so a category can return more than three products.
Columns, in this order: category, product name, revenue, rank within the category (1 is highest; ties share a rank and the next rank is not skipped).
Order by category, then rank, then product name.
Examples
Constraints
- Products with no sales still compete, with revenue 0.
- Ranks have no gaps.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.