FastPrepRestaurant Waitlist with Exact-Size Tables

Restaurant Waitlist with Exact-Size Tables

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Process operations against an initially empty restaurant waitlist.

  • JOIN id size appends a party unless that ID is already waiting.
  • LEAVE id removes that party when present and otherwise does nothing.
  • SERVE tableSize removes and returns the earliest waiting party whose size is exactly tableSize, or NONE if no exact-size party waits.

An ID may join again after leaving or being served. Only SERVE operations produce output.

Function

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

Examples

Example 1

operations = [["JOIN","A","4"],["JOIN","B","2"],["JOIN","C","4"],["SERVE","4"],["SERVE","4"]]return = ["A","C"]

Exact-size parties are served in their join order.

Example 2

operations = [["JOIN","A","3"],["JOIN","A","3"],["LEAVE","A"],["LEAVE","A"],["SERVE","3"],["JOIN","A","3"],["SERVE","3"]]return = ["NONE","A"]

Duplicate operations are idempotent, and A may rejoin after leaving.

Example 3

operations = []return = []

No operations produce no service results.

Constraints

  • 0 <= operations.length <= 200000.
  • Party IDs contain 1 through 20 letters or digits.
  • Party and table sizes are integers in [1,100000].
  • Operation rows have the exact arity shown above.

More Google problems

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