FastPrepRestaurant Waitlist

Restaurant Waitlist

Google logoGoogle● HardNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Process operations against an initially empty restaurant waitlist.

  • JOIN id size adds a party with a unique string ID and positive party size to the back of the waitlist.
  • DELETE id removes the named party, which is guaranteed to be waiting.
  • SERVE tableSize removes and returns the earliest still-waiting party whose size is at most tableSize. Return NONE when no party fits.

Only SERVE operations produce output. Return those party IDs in operation order.

Function

processWaitlist(operations: String[][]) → String[]

Examples

Example 1

operations = [["JOIN","A","4"],["JOIN","B","2"],["JOIN","C","6"],["SERVE","3"],["DELETE","A"],["SERVE","6"],["SERVE","10"]]return = ["B","C","NONE"]

Party B is the first party that fits a table of size 3. After deleting A, party C is served next.

Example 2

operations = [["JOIN","P1","5"],["JOIN","P2","2"],["JOIN","P3","2"],["SERVE","2"],["SERVE","5"]]return = ["P2","P1"]

P2 is earlier than P3 among parties that fit size 2. The next table can seat the earlier remaining party P1.

Constraints

  • 1 <= operations.length <= 100000.
  • Party IDs contain letters or digits, have length from 1 through 20, and are unique across the entire operation sequence.
  • Party sizes and table sizes are integers from 1 through 100000.
  • Every DELETE operation names a currently waiting party.

More Google problems

See Google hiring insights
public String[] processWaitlist(String[][] operations) {
    // Write your code here.
}
operations[["JOIN","A","4"],["JOIN","B","2"],["JOIN","C","6"],["SERVE","3"],["DELETE","A"],["SERVE","6"],["SERVE","10"]]
expected["B", "C", "NONE"]
Checking account…