Compute buffet revenue with capacity and waiting
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates event-driven simulation and state-management skills, including handling customer lifecycle, capacity constraints, FIFO queuing behavior, and revenue aggregation.
Constraints
- 0 <= capacity <= 200000
- 0 <= n = len(prices) <= 200000
- 0 <= m = len(events) <= 200000
- 0 <= prices[i] <= 10^9
- 0 <= events[j] < n
- For each customer, appearances in `events` alternate arrival, leave, arrival, leave, ... starting with arrival
Examples
Input: (2, [10, 20, 30], [0, 1, 2, 0, 2])
Expected Output: 60
Explanation: Customers 0 and 1 are seated first and pay 10 + 20. Customer 2 waits, then gets seated when customer 0 leaves and pays 30. Total revenue is 60.
Input: (1, [5, 7, 9], [0, 1, 1, 0])
Expected Output: 5
Explanation: Customer 0 is seated and pays 5. Customer 1 waits, then leaves before ever being seated, so they pay nothing.
Input: (1, [8], [0, 0, 0, 0])
Expected Output: 8
Explanation: Customer 0 visits twice. They pay only the first time they are seated, so the total stays 8.
Input: (2, [5, 10, 15, 20], [0, 1, 2, 3, 2, 0, 2, 1, 3, 2])
Expected Output: 50
Explanation: Customers 0 and 1 pay first. Customer 2 waits, customer 3 waits, then customer 2 leaves the queue. When 0 leaves, 3 is seated and pays. Later 2 arrives again, waits, and is seated when 1 leaves, paying then. Total is 5 + 10 + 20 + 15 = 50.
Input: (0, [4, 6], [0, 1, 0, 1])
Expected Output: 0
Explanation: There is no seating capacity, so everyone only waits and eventually leaves. No one is ever seated, so revenue is 0.
Input: (3, [2, 3], [])
Expected Output: 0
Explanation: No events means no arrivals and no revenue.
Hints
- Track each customer's current state: absent, seated, or waiting.
- A plain FIFO queue is not enough, because a waiting customer may leave before reaching the front. Use a data structure that preserves order and also supports fast removal by customer ID.