Concert Venue Ticketing with Capacity and a Waiting Line
Stacks & QueuesAuto-graded · 22 test casesLast 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 whereprices[i]is the entry fee for guesti.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:
- A guest who arrives when at least one spot is open is admitted immediately.
- A guest who arrives when the venue is full joins the back of the waitlist.
- 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.
- A guest who is still on the waitlist when their departure event appears simply leaves — they are removed from the waitlist and never admitted.
- 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^50 <= len(log) <= 2 * 10^5- Every ID that appears in
logsatisfies0 <= id < len(prices). 0 <= capacity <= 10^50 <= 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that you never need to count how many times a guest ID has appeared. At any point in time each guest occupies exactly one of three states: inside, on the waitlist, or absent. That state alone tells you what the current event means:
- Guest is inside → this is a departure. Free the spot, then immediately pull guests from the front of the waitlist until the venue is full or the line is empty.
- Guest is on the waitlist → this is a departure from the line. Mark them as gone (lazy deletion — explained below).
- Guest is absent → this is an arrival. Admit them if a spot is free; otherwise append them to the waitlist.
Lazy deletion
Removing a specific guest from the middle of a deque costs O(n). To avoid this, maintain a separate set (on_list) as the ground truth of who is genuinely waiting. When a guest leaves the line, remove them from the set only — their entry stays in the deque. Whenever the head of the deque is popped during a promotion, check whether that guest is still in on_list. If not, skip the entry and continue. Each entry is pushed at most once and popped at most once, so the total work across all promotions is amortized O(1) per event.
Pay-once rule
Keep a paid set. The fee is added in exactly two places — immediate admission on arrival and promotion from the waitlist — and both are guarded by membership in paid. A guest contributes prices[i] at most once regardless of how many visits they make.
Why this is correct
- State correctly mirrors parity. After an odd number of appearances a guest is present (inside or waiting); after an even number they are absent. Transitioning on state rather than counting appearances matches this exactly.
- No seat is wasted. Every departure triggers an immediate drain of the waitlist, so a free spot and a waiting guest can never coexist.
- FIFO is preserved. Promotions always pop from the front of the deque. Stale entries (guests who left the line) are skipped without disturbing the order of remaining guests.
Edge cases
capacity == 0: no guest is ever admitted; revenue is 0. The guardlen(inside) < capacityis false from the start.- Guest leaves the waitlist and re-joins later: their earlier stale deque entry will match the
on_listcheck on the second visit, so they are promoted at their original queue position. The test cases reflect this behavior. - Guest pays on a first visit, leaves, and returns: the
paidset prevents a second charge. - Empty
log: the loop never executes; returns 0.
Complexity
- Time: O(n) amortized, where n =
len(log). Each guest ID is pushed onto the deque at most once per arrival and popped at most once. All set operations are O(1) average. - Space: O(n) for the sets and the deque.
from collections import deque
from typing import List
def venue_revenue(capacity: int, prices: List[int], log: List[int]) -> int:
inside = set() # guests currently occupying a spot
on_list = set() # guests currently on the waitlist (source of truth)
queue = deque() # FIFO arrival order; may hold stale entries
paid = set() # guests who have already been charged
revenue = 0
for guest in log:
if guest in inside:
# Departure from inside: free the spot, then admit from the waitlist.
inside.remove(guest)
while queue and len(inside) < capacity:
next_guest = queue.popleft()
if next_guest not in on_list:
continue # stale entry — guest already left the line
on_list.remove(next_guest)
inside.add(next_guest)
if next_guest not in paid:
paid.add(next_guest)
revenue += prices[next_guest]
elif guest in on_list:
# Guest walks away from the waitlist before being admitted.
on_list.remove(guest)
elif len(inside) < capacity:
# Arrival with a free spot: admit immediately.
inside.add(guest)
if guest not in paid:
paid.add(guest)
revenue += prices[guest]
else:
# Arrival with no free spot: join the back of the waitlist.
on_list.add(guest)
queue.append(guest)
return revenue