Booking Concert Tickets in Groups - Problem

A concert hall has n rows numbered from 0 to n - 1, each with m seats, numbered from 0 to m - 1. You need to design a ticketing system that can allocate seats in the following cases:

  • If a group of k spectators can sit together in a row.
  • If every member of a group of k spectators can get a seat. They may or may not sit together.

Note that the spectators are very picky. Hence:

  • They will book seats only if each member of their group can get a seat with row number less than or equal to maxRow. maxRow can vary from group to group.
  • In case there are multiple rows to choose from, the row with the smallest number is chosen. If there are multiple seats to choose in the same row, the seat with the smallest number is chosen.

Implement the BookMyShow class:

  • BookMyShow(int n, int m) Initializes the object with n as number of rows and m as number of seats per row.
  • int[] gather(int k, int maxRow) Returns an array of length 2 denoting the row and seat number (respectively) of the first seat being allocated to the k members of the group, who must sit together. In other words, it returns the smallest possible r and c such that all [c, c + k - 1] seats are valid and empty in row r, and r <= maxRow. Returns [] in case it is not possible to allocate seats to the group.
  • boolean scatter(int k, int maxRow) Returns true if all k members of the group can be allocated seats in rows 0 to maxRow, who may or may not sit together. If the seats can be allocated, it allocates k seats to the group with the smallest row numbers, and the smallest possible seat numbers in each row. Otherwise, returns false.

Input & Output

Example 1 — Basic Operations
$ Input: operations = [["BookMyShow",2,5],["gather",4,0],["gather",2,0],["scatter",5,1],["scatter",5,1]]
Output: [null,[0,0],[0,4],true,false]
💡 Note: Initialize 2×5 hall. First gather(4,0) allocates seats 0-3 in row 0. Second gather(2,0) fails (row 0 full). Scatter(5,1) uses remaining seat in row 0 plus 4 in row 1. Second scatter(5,1) fails (no seats left).
Example 2 — Multiple Rows
$ Input: operations = [["BookMyShow",3,3],["gather",3,1],["scatter",2,2]]
Output: [null,[0,0],true]
💡 Note: Initialize 3×3 hall. Gather(3,1) finds 3 consecutive seats in row 0. Scatter(2,2) places 2 seats in row 1.
Example 3 — Edge Case
$ Input: operations = [["BookMyShow",1,1],["gather",1,0],["gather",1,0]]
Output: [null,[0,0],[]]
💡 Note: Single seat hall. First gather succeeds, second fails as hall is full.

Constraints

  • 1 ≤ n ≤ 5 × 104
  • 1 ≤ m ≤ 109
  • 1 ≤ k ≤ min(maxRow + 1, n) × m
  • 0 ≤ maxRow ≤ n - 1
  • At most 5 × 104 calls to gather and scatter

Visualization

Tap to expand
INPUTALGORITHMRESULT0123XR0:01234R1:Hall: 2 rows × 5 seatsRequest: gather(4, maxRow=0)1Build Segment Trees2Query: Row 0 max=4 ≥ 4 ✓3Allocate seats 0-3 in Row 04Update segment tree valuesMax TreeSum TreeSeat Allocation[0, 0]Row 0, starting at seat 04 consecutive seats foundTime: O(log n + m)Key Insight:Use segment trees to efficiently find rows with enough consecutive or total available seatsTutorialsPoint - Booking Concert Tickets in Groups | Segment Tree Approach
Asked in
Google 25 Amazon 18 Microsoft 15 Facebook 12
15.4K Views
Medium Frequency
~45 min Avg. Time
387 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