KatInterview

Restaurant Queue Tracker with Fast Rank Lookup

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designdesignorder-statistictreap

Last updated

Replay a list of queue operations on a first-come waitlist, supporting add, remove-by-id, rank lookup and front peek, with rank queries faster than a linear scan.

Restaurant Queue Tracker

A restaurant keeps a queue of parties waiting for a table. Each party has a unique positive integer party_id and a head count. Parties are served in arrival order, but a party can also be removed from anywhere in the queue when it is seated.

You are given a batch of operations to replay in order against an initially empty queue. Return one result per operation.

Parameters

  • operations: a list of operation names.
  • arguments: a list of the same length. arguments[i] holds the arguments for operations[i].

Operations

OperationArgumentsEffectResult
add_party[party_id, num_people]Put the party at the back of the queue, unless that id is already waiting.True if added, False if the id is already in the queue
seat_party[party_id]Remove the party from the queue.True if removed, False if the id is not in the queue
get_position[party_id]Look up the party's current place in the queue, counting from 1 at the front.The 1-based position, or -1 if the id is not in the queue
get_next_party[]Peek at the party at the front without removing it.The front party_id, or -1 if the queue is empty

Rules

  • num_people is stored conceptually but never changes any result.
  • Once a party has been seated, its id is no longer in the queue, so it may be added again later and then joins at the back.
  • A rejected add_party (duplicate id) leaves the queue unchanged.
  • Removing a party shifts everyone behind it forward by one position.

Return the list of results in the same order as the operations.

Constraints

  • 1 <= len(operations) == len(arguments) <= 10^5
  • Every party_id is a positive integer.
  • Operation names are exactly one of add_party, seat_party, get_position, get_next_party.
  • A position lookup should run faster than scanning the whole queue (aim for roughly O(log n) per operation).

Examples

Example 1

Input: replay_waitlist(['add_party', 'add_party', 'get_position', 'seat_party', 'get_position', 'get_next_party'], [[101, 4], [102, 2], [102], [101], [102], []])

Output: [True, True, 2, True, 1, 102]

Hints

Hint 1

A plain list or deque gives O(1) access to the front, but finding a party's rank or deleting from the middle costs O(n). A hash map of indices breaks because every removal shifts the later indices.

Hint 2

You need an ordered sequence that can answer 'how many elements come before this one?' and delete a specific element. Think of a balanced tree where each node stores its subtree size.

Hint 3

Alternative: assign each arrival an increasing sequence number and use a Fenwick tree over those numbers (1 for waiting, 0 for gone). Position is a prefix sum, and the front is the first set index. Keep a map from id to sequence number.