Booking Concert Tickets in Groups - Problem
Design a Concert Ticketing System
You're building a ticketing system for a popular concert venue! The concert hall has
Your system must handle two types of booking requests:
๐ซ Group Seating (gather): A group of
๐ซ Flexible Seating (scatter): A group of
The customers are picky though! They have these requirements:
โข Only consider rows 0 through
โข Always prefer the smallest row number available
โข Within a row, prefer seats with smallest numbers
Your Task:
Implement
โข
โข
You're building a ticketing system for a popular concert venue! The concert hall has
n rows (numbered 0 to n-1) with m seats each (numbered 0 to m-1).Your system must handle two types of booking requests:
๐ซ Group Seating (gather): A group of
k people want to sit together in the same row๐ซ Flexible Seating (scatter): A group of
k people are okay sitting apart, just want seatsThe customers are picky though! They have these requirements:
โข Only consider rows 0 through
maxRowโข Always prefer the smallest row number available
โข Within a row, prefer seats with smallest numbers
Your Task:
Implement
BookMyShow class with:โข
gather(k, maxRow): Returns [row, seat] of first seat for k consecutive seats, or [] if impossibleโข
scatter(k, maxRow): Returns true if k seats can be allocated (anywhere), false otherwise Input & Output
example_1.py โ Basic Operations
$
Input:
["BookMyShow", "gather", "gather", "scatter", "scatter"]
[[2, 5], [4, 0], [2, 0], [5, 1], [5, 1]]
โบ
Output:
[null, [0, 0], [], true, false]
๐ก Note:
BookMyShow bms = new BookMyShow(2, 5); // 2 rows, 5 seats each
bms.gather(4, 0); โ [0, 0] (4 seats in row 0, starting at seat 0)
bms.gather(2, 0); โ [] (row 0 only has 1 seat left, can't fit 2 consecutive)
bms.scatter(5, 1); โ true (can scatter 5 people across rows 0-1)
bms.scatter(5, 1); โ false (not enough total seats remaining)
example_2.py โ MaxRow Constraint
$
Input:
["BookMyShow", "gather", "gather", "gather"]
[[3, 4], [2, 1], [3, 2], [2, 2]]
โบ
Output:
[null, [0, 0], [1, 0], [2, 0]]
๐ก Note:
BookMyShow bms = new BookMyShow(3, 4); // 3 rows, 4 seats each
bms.gather(2, 1); โ [0, 0] (row 0 available, fits 2 consecutive seats)
bms.gather(3, 2); โ [1, 0] (row 0 has only 2 seats left, use row 1)
bms.gather(2, 2); โ [2, 0] (rows 0,1 don't have enough consecutive seats)
example_3.py โ Scatter vs Gather
$
Input:
["BookMyShow", "scatter", "gather", "scatter"]
[[2, 3], [4, 1], [2, 1], [1, 1]]
โบ
Output:
[null, true, [], true]
๐ก Note:
BookMyShow bms = new BookMyShow(2, 3); // 2 rows, 3 seats each
bms.scatter(4, 1); โ true (scatter 4 people across rows 0-1: fills row 0 + 1 seat in row 1)
bms.gather(2, 1); โ [] (no row has 2 consecutive seats after scattering)
bms.scatter(1, 1); โ true (1 seat still available in row 1)
Time & Space Complexity
Time Complexity
O(n*m)
Each gather() or scatter() operation may need to scan all n*m seats
โ Linear Growth
Space Complexity
O(n*m)
2D array to store seat occupancy status
โก Linearithmic Space
Constraints
- 1 โค n โค 5 ร 104
- 1 โค m, k โค 109
- 0 โค maxRow โค n - 1
- At most 5 ร 104 calls will be made to gather and scatter
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code