← Questions

Waitlist with top-K and bottom-K queries

0
GoogleCodingLast asked Feb 2026

A coding interview question asked in Google's first round (coding), last asked Feb 2026. It tests data structure design and ordered sets.

Build the waitlist for a busy restaurant. Parties join the end of the line, may leave at any time, and are seated from the front. Staff need two views at any moment: the first k parties in line and the last k parties in line.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • join(party_id: str) -> bool — adds the party at the back; False if it is already waiting.
  • leave(party_id: str) -> bool — removes the party wherever it is; False if it is not waiting.
  • seat() -> str | None — removes and returns the party at the front.
  • top_k(k: int) -> list[str] — the first k parties, front first.
  • bottom_k(k: int) -> list[str] — the last k parties, in line order.

Example

join("A"); join("B"); join("C"); join("D")
leave("B")      ->  True
top_k(2)        ->  ["A", "C"]
bottom_k(2)     ->  ["C", "D"]
seat()          ->  "A"
top_k(5)        ->  ["C", "D"]

Constraints

  • Up to 10^5 operations; k is usually small compared with the line.

Clarifying questions

  • Is the order strictly by join time, or do some parties have priority (size, reservations)?
  • What does k larger than the line return?
  • Can a party that left join again, and at which position?

What a strong answer covers

  • A doubly linked list for the line plus a hash map from party id to its node: join, leave and seat in O(1).
  • top_k walking from the head and bottom_k walking from the tail, O(k).
  • Class design: a Waitlist with clear methods and a Party record; invalid operations return False instead of raising.
  • Tests: leaving from the front, middle and back; queries on an empty line.

Follow-ups

  • Parties have a priority: switch to a balanced tree or sorted set keyed by (priority, join_time), with O(log n) updates and O(k + log n) queries.
  • Show each party its position in line in O(log n).
  • Estimate each party's wait time.

Where it's asked

  • Google · First round: coding · Last asked Feb 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

data structure design · ordered sets

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