← Questions

Live top-k best-selling books leaderboard

0
SnowflakeUberCodingLast asked May 2026

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: adds quantity copies to the book's running total and returns the new total. quantity is a positive integer.
  • get_sales(book_id: str) -> int: returns the book's total, or 0 if it has never sold.
  • top_k(k: int) -> List[str]: returns up to k book ids with the highest totals, best first. Break ties by the smaller book id. If fewer than k books have sales, return all of them. Return [] when k <= 0.

Constraints

  • Up to 10^5 distinct books and 10^6 calls in total.
  • top_k may be called as often as record_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_k called compared with record_sale, and how large is k usually?
  • Is quantity always positive, so totals only grow, or can a sale be reversed?
  • Is the tie-break plain string comparison of book ids?
  • Must top_k reflect 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_sale removes the old key and inserts the new one in O(log n); top_k reads the first k entries 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_sales O(1).
  • Handles k <= 0, k larger than the number of books, an unknown book in get_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

heap · ordered set · hash map · design

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding