Katkat

Compute buffet revenue with capacity and waiting

UpstartSoftware Engineer · Technical Screen

Last 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:

  1. prices: an array where prices[i] is how much customer i is willing to pay.
  2. events: an array of customer IDs. For each customer id, the 1st time id appears 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