← Questions
Waitlist with top-K and bottom-K queries
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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;Falseif it is already waiting. -
leave(party_id: str) -> bool— removes the party wherever it is;Falseif it is not waiting. -
seat() -> str | None— removes and returns the party at the front. -
top_k(k: int) -> list[str]— the firstkparties, front first. -
bottom_k(k: int) -> list[str]— the lastkparties, 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;
kis 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
klarger 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,leaveandseatin O(1). -
top_kwalking from the head andbottom_kwalking from the tail, O(k). - Class design: a
Waitlistwith clear methods and aPartyrecord; invalid operations returnFalseinstead 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
