Compute buffet revenue with capacity and waiting
UpstartSoftware Engineer · Technical ScreenLast updated
This question evaluates event-driven simulation and state-management skills, including handling customer lifecycle, capacity constraints, FIFO queuing behavior, and revenue aggregation.
A casino buffet has a maximum capacity capacity (number of customers that can be seated at once).
You are given:
prices: an array whereprices[i]is how much customeriis willing to pay.events: an array of customer IDs. For each customerid, the 1st timeidappears means the customer arrives; the 2nd time means the customer leaves; the 3rd time arrives again; the 4th time leaves again; etc. (Arrivals and departures are interleaved across customers.)
Rules:
- If a customer arrives and there is free capacity, they are seated immediately.
- If there is no free capacity, they wait in a FIFO queue.
- When a seated customer leaves, newly freed seats are immediately offered to waiting customers in FIFO order.
- If a waiting customer leaves before being seated, they simply leave the queue.
- Each customer pays at most once total (the first time they get seated). Subsequent visits, even if seated again, do not add revenue.
Return the total buffet revenue.
Example event interpretation:
events = [0, 1, 2, 0, 2]means: 0 arrives → 1 arrives → 2 arrives → 0 leaves → 2 leaves.
Implement a function:
- Input:
int capacity, int[] prices, int[] events - Output:
long totalRevenue
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.