KatInterview

Concert Venue Ticketing with Capacity and a Waiting Line

Stacks & QueuesAuto-graded · 22 test cases
MediumAuto-gradedStacks & Queuessimulationqueuehash-set

Last updated

Tests event-driven simulation skills: tracking visitor state, enforcing a seating capacity, managing a FIFO waitlist, and collecting one-time entry fees.

A concert venue has a fixed number of spots, given by capacity. Once all spots are taken, arriving guests line up outside in the order they arrive.

You receive two inputs:

  • prices: a list where prices[i] is the entry fee for guest i.
  • log: a list of guest IDs representing a sequence of gate events. Each ID's first appearance in the log is an arrival; its second appearance is a departure; its third is another arrival; its fourth is another departure; and so on.

The venue operates by these rules:

  1. A guest who arrives when at least one spot is open is admitted immediately.
  2. A guest who arrives when the venue is full joins the back of the waitlist.
  3. As soon as a guest leaves, every newly freed spot is filled right away by taking guests from the front of the waitlist, one at a time, until either the venue is full again or the waitlist is empty.
  4. A guest who is still on the waitlist when their departure event appears simply leaves — they are removed from the waitlist and never admitted.
  5. Each guest pays at most once, the first time they are actually admitted through the door. Being admitted on a return visit after already having paid once does not generate additional revenue.

Return the total revenue collected.

Constraints

  • 1 <= len(prices) <= 10^5
  • 0 <= len(log) <= 2 * 10^5
  • Every ID that appears in log satisfies 0 <= id < len(prices).
  • 0 <= capacity <= 10^5
  • 0 <= prices[i] <= 10^4
  • Arrival and departure events for different guests may be freely interleaved.
  • A guest's events always alternate strictly between arrival and departure.

Examples

Example 1

Input: venue_revenue(2, [10, 20, 30], [0, 1, 2, 0, 2])

Output: 60

Example 2

Input: venue_revenue(1, [5, 7], [0, 1, 0, 1])

Output: 12

Example 3

Input: venue_revenue(2, [4, 8, 12], [0, 1, 2, 2, 0, 2])

Output: 24

Hints

Hint 1

At any moment, every guest is in exactly one of three states: inside the venue, on the waitlist, or absent entirely. Use that state — not an occurrence counter — to decide what each gate event means.

Hint 2

Removing a guest from the middle of a queue is expensive. Instead, keep a separate set as the ground truth of who is currently waiting. When someone walks away from the waitlist, remove them only from the set and skip their entry lazily the next time it surfaces at the front of the queue.

Hint 3

Revenue and admission are separate concerns. Maintain a 'has paid' set and charge a guest only the first time they step through the door, regardless of how many times they visit.