Live top-k best-selling books leaderboard
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's coding round and at Snowflake, last asked May 2026. It tests heap, ordered set and hash map.
A bookstore wants a live leaderboard of its best-selling books. Sales arrive one at a time, and the leaderboard can be read at any moment.
Implement a BookSalesTracker class:
-
BookSalesTracker(): creates an empty tracker. -
record_sale(book_id: str, quantity: int = 1) -> int: addsquantitycopies to the book's running total and returns the new total.quantityis a positive integer. -
get_sales(book_id: str) -> int: returns the book's total, or0if it has never sold. -
top_k(k: int) -> List[str]: returns up tokbook ids with the highest totals, best first. Break ties by the smaller book id. If fewer thankbooks have sales, return all of them. Return[]whenk <= 0.
Constraints
- Up to 10^5 distinct books and 10^6 calls in total.
-
top_kmay be called as often asrecord_sale, so avoid sorting every book on each read.
Example
t = BookSalesTracker()
t.record_sale("dune", 5) # 5
t.record_sale("emma", 3) # 3
t.record_sale("beloved", 3) # 3
t.top_k(2) # ["dune", "beloved"] (tie at 3 goes to the smaller id)
t.record_sale("emma", 4) # 7
t.top_k(3) # ["emma", "dune", "beloved"]
t.top_k(10) # ["emma", "dune", "beloved"]
t.get_sales("missing") # 0
Interviewers often add parts: returns that lower a total, a leaderboard over only the last hour, per-category leaderboards, or a tracker spread across many machines.
Clarifying questions
- How often is
top_kcalled compared withrecord_sale, and how large iskusually? - Is
quantityalways positive, so totals only grow, or can a sale be reversed? - Is the tie-break plain string comparison of book ids?
- Must
top_kreflect every sale immediately, or is a leaderboard a few seconds stale acceptable? - Is the tracker used from one thread only?
What a strong answer covers
- A hash map from book to total plus an ordered structure keyed by
(-total, book_id)(a balanced tree or sorted list).record_saleremoves the old key and inserts the new one in O(log n);top_kreads the firstkentries in O(k) after an O(log n) seek. - Compares the alternatives: sorting on every read (O(n log n)), a heap built per call (O(n log k)), and a fixed-size min-heap, which only works for one fixed
k. - Gets the tie rule right when a book's new total equals another's, and keeps
get_salesO(1). - Handles
k <= 0,klarger than the number of books, an unknown book inget_sales, and many updates to the same book. - Tests the example sequence step by step, then compares against a brute-force sort on random sales.
Follow-ups
- Returns that lower a total: which structures still work when totals can go down?
- A leaderboard over only the last hour: bucket sales by minute and subtract buckets as they expire.
- Per-category leaderboards when one book belongs to several categories.
- Many machines: shard by book, merge each shard's top
k, and say when an approximate answer (heavy-hitter sketches) is acceptable.
Where it's asked
- Uber · Coding · Last asked May 202660 min · Two rounds; one is often object-oriented design (a parking lot, a meeting scheduler, an expression evaluator) · No AI, except a newer AI-assisted debugging round in some senior loops
- Snowflake · Coding · Last asked Feb 202660 min · Live coding; senior loops may agree a small storage component together, then build it
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

