[Solution Discussion] Restaurant Wait List

There's this question posted by someone sometime back https://leetcode.com/discuss/interview-question/1779091/Google-or-Phone-Interview-or-USA-or-Restaurant-Wait-List

Design a wait list for customers at restaurant:

  • Add customers to wait list (for example: Bob, party of 4 people)
  • Remove a customer from wait list
  • Given a open table with N seats, remove and return the first customer party of size N

I have a solution for this question using a LinkedHashSet....can someone tell me if this will work or a better solution we can have for this?
I got similar type of question in another company's interview a week back....

Solution:

// array index is table size and LinkedHashSet will store each customer's waiting for it.....taking table sizes 1 to 10
LinkedHashSet<String>[] tableSizeWaitMap = new LinkedHashSet[10];

1. Add Customer "X" with party size 5
tableSizeWaitMap[5].add("X");   // O(1)

2. Remove Customer "X" with party size 5
tableSizeWaitMap[5].remove("X");  // O(1)

3. Assign customer to table for size 5
tableSizeWaitMap[5].iterator().next();    // will return first inserted customer in the set O(1)


// In case table size are not in 1 to N order, we can take HashMap<LinkedHashSet<String>>
Comments (0)