Google questions

Restaurant Waitlist

GoogleOnsiteMedium

A restaurant keeps a waiting list of parties. Each party has a name and a number of people. Parties are served in the order they joined, but a party at the front can be passed over when it is too big for the free table.

Part 1: Waitlist operations

Implement the Waitlist class.

  • join(name, party_size) adds the party to the back of the list.
  • delete(name) removes the party with that name. It does nothing if no such party is waiting.
  • find_first_match(table_size) returns the name of the party that joined earliest among those with party_size <= table_size, or None if there is none. It does not remove the party.

A name is on the list at most once at a time. A party that has left can join again, and it then joins at the back like any new party.

join and delete return None.

  1. Example 1
    init
    []
    operations
    [["join",["ann",4]],["join",["bo",2]],["join",["cy",6]],["find_first_match",[3]],["find_first_match",[1]],["find_first_match",[6]],["delete",["ann"]],["find_first_match",[6]],["find_first_match",[6]],["delete",["bo"]],["find_first_match",[3]],["find_first_match",[10]]]
    Output
    [null,null,null,"bo",null,"ann",null,"bo","bo",null,null,"cy"]

    Why: bo is the first party of size at most 3, so find_first_match(3) returns bo; nobody has a party of size 1. Table 6 fits ann first, and asking again returns ann because finding does not remove. After ann leaves, bo is first, and once bo leaves too, table 3 fits nobody and table 10 gets cy.

Constraints

  • 1 <= len(operations) <= 10^5
  • 1 <= len(name) <= 10
  • 1 <= party_size <= 10^6
  • 1 <= table_size <= 10^6
  • join is never called with a name that is already waiting
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Restaurant Waitlist: Google interview question | LoopCoach