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 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 seats

The 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

n
2n
โœ“ Linear Growth
Space Complexity
O(n*m)

2D array to store seat occupancy status

n
2n
โšก 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
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen