Skip to main content

Cinema Seat Allocation - Solution & Explanation

MediumArrayHash TableGreedyBit Manipulation20 min readAsked at: Microsoft, LinkedIn, Bloomberg +2
Practice this problem

Problem Statement

A cinema has n rows of seats, numbered from 1 to n and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above.

Given the array reservedSeats containing the numbers of seats already reserved, for example, reservedSeats[i] = [3,8] means the seat located in row 3 and labelled with 8 is already reserved.

Return the maximum number of four-person groups you can assign on the cinema seats. A four-person group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle split a four-person group, in that case, the aisle split a four-person group in the middle, which means to have two people on each side.

 

Example 1:

Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
Output: 4
Explanation: The figure above shows the optimal allocation for four groups, where seats mark with blue are already reserved and contiguous seats mark with orange are for one group.

Example 2:

Input: n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
Output: 2

Example 3:

Input: n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
Output: 4

 

Constraints:

  • 1 <= n <= 10^9
  • 1 <= reservedSeats.length <= min(10*n, 10^4)
  • reservedSeats[i].length == 2
  • 1 <= reservedSeats[i][0] <= n
  • 1 <= reservedSeats[i][1] <= 10
  • All reservedSeats[i] are distinct.

Approach Overview

Problem Overview: A cinema has n rows with 10 seats per row. Some seats are already reserved. A family of four must sit together in one of three valid blocks: [2,3,4,5], [4,5,6,7], or [6,7,8,9]. The task is to compute the maximum number of families that can be seated without breaking these constraints.

The key observation: each row can normally hold two families (left block 2-5 and right block 6-9). Reserved seats only affect the rows where they appear, so you never need to simulate all n rows when n can be as large as 1e9. Instead, track only rows that contain reservations using a hash table.

Approach 1: Greedy Approach Using Logical Checks (O(r) time, O(r) space)

Group reserved seats by row using a map. For each affected row, check whether the three valid family blocks are free. Use simple logical checks to see if seats in ranges 2-5, 4-7, or 6-9 contain any reservation. If both left and right blocks are free, place two families. Otherwise check if the middle block 4-7 is free and place one family if possible. Rows without reservations automatically contribute two families. This approach relies on direct seat checks with an array or set lookup and works well because each row has only a constant number of seat groups to evaluate.

Approach 2: Bitmask Representation for Each Row (O(r) time, O(r) space)

Instead of storing seat numbers directly, encode reserved seats for each row as a bitmask. Each bit represents whether a seat from 2 to 9 is occupied. Bitwise AND operations quickly test whether a candidate family block conflicts with reservations. Precompute masks for the three valid seat groups (2-5, 4-7, 6-9). For each row mask, check conflicts using bit operations like (mask & groupMask) == 0. Bitmasks make the checks extremely fast and compact, which is why this solution often appears in competitive programming and bit manipulation interview problems.

Recommended for interviews: The greedy row-by-row check is usually the first solution interviewers expect because it clearly models the seating rules. The bitmask version demonstrates deeper optimization and familiarity with low-level operations. Both achieve O(r) time where r is the number of reserved seats (or rows containing reservations), which is optimal since only those rows need processing.

Approach 1: Bitmask Representation for Each Row

This approach models each row using a bitmask representation where each of the 10 seats in a row is represented by a bit. A bit is set if the corresponding seat is reserved. By doing this, you can efficiently determine the possible configurations for placing groups of four seats, which are specific patterns in the bitmask. You can then count the maximum number of 4-person groups that can be seated in each row based on the bitmask state of that row. Focus on the segments: (2,3,4,5), (4,5,6,7), and (6,7,8,9).

This implementation uses a dictionary to store reserved seat information for each row, where the key is the row number and the value is a bitmask indicating the reserved seats. It then calculates the maximum number of four-person groups that can fit in each row by checking which of the specified seat configurations are available.

Code

Python

Java

C++

C#

JavaScript

Complexity

Time Complexity: O(m), where m is the length of the reservedSeats list. Space Complexity: O(m) for storing the reserved seat information for each row.

Try this approach in the editor →

Approach 2: Greedy Approach Using Logical Checks

This approach uses logical conditions based on seat availability for every row and assesses possible seating blocks within each row independently. Rows without reserved seats can instantly allocate two groups. For rows with reserved seats, logical checks are performed to ascertain feasible places to position groups of four, mainly positions: (2-5), (4-7), and (6-9).

This implementation starts by mapping reserved seats per row. It employs logical checks to decide whether rows can fit groups of four in the specific (2-5), (4-7), or (6-9) seat blocks, then updates the maximum groups count per row accordingly.

Code

Python

Java

C++

C#

JavaScript

Complexity

Time Complexity: O(m), where m represents the reserved seat array size. Space Complexity: O(m), due to processing and storing the reserved seats by row.

Try this approach in the editor →

Approach 3: Hash Table + Bit Manipulation

We use a hash table d to store all the reserved seats, where the key is the row number, and the value is the state of the reserved seats in that row, i.e., a binary number. The j-th bit being 1 means the j-th seat is reserved, and 0 means the j-th seat is not reserved.

We traverse reservedSeats, for each seat (i, j), we add the state of the j-th seat (corresponding to the 10-j bit in the lower bits) to d[i].

For rows that do not appear in the hash table d, we can arrange 2 families arbitrarily, so the initial answer is (n - len(d)) times 2.

Next, we traverse the state of each row in the hash table. For each row, we try to arrange the situations 1234, 5678, 3456 in turn. If a situation can be arranged, we add 1 to the answer.

After the traversal, we get the final answer.

The time complexity is O(m), and the space complexity is O(m). Where m is the length of reservedSeats.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Bitmask Representation for Each Row

Time Complexity: O(m), where m is the length of the reservedSeats list. Space Complexity: O(m) for storing the reserved seat information for each row.

Greedy Approach Using Logical Checks

Time Complexity: O(m), where m represents the reserved seat array size. Space Complexity: O(m), due to processing and storing the reserved seats by row.

Hash Table + Bit Manipulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy Logical Seat ChecksO(r)O(r)Best for readability and interviews when you want a clear rule-based solution
Bitmask Representation per RowO(r)O(r)Preferred when optimizing seat checks with fast bit operations

Video Solution

Cinema Seat Allocation (Leetcode 1386)Coding Interviews5,722 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Cinema Seat Allocation easy or hard?
Cinema Seat Allocation is classified as a medium difficulty problem. The challenge comes from recognizing that only three seat groups matter and that rows without reservations can be counted immediately, reducing the problem to simple greedy or bitmask checks.
Cinema Seat Allocation Python/Java solution
Python, Java, C++, C#, and JavaScript implementations usually follow the same structure: build a map of reserved seats by row, check the valid seat blocks, and count the number of families per row. The bitmask variant replaces seat lists with integer masks for faster checks.
How to solve Cinema Seat Allocation in O(n)?
Store reserved seats grouped by row using a hash table. For each row, check the availability of the three valid family seat groups (2-5, 4-7, 6-9). Rows without reservations allow two families immediately. Each row requires constant checks, giving overall O(r) time.
What is the best approach for Cinema Seat Allocation?
The most practical solution uses a greedy row-by-row check with a hash map storing reserved seats per row. For each row, verify whether the seat blocks 2-5, 4-7, and 6-9 are free. This runs in O(r) time where r is the number of reserved seats and avoids processing all n rows when n can be very large.
Is Cinema Seat Allocation asked at Google/Amazon/Meta?
Cinema Seat Allocation is a common medium-level interview problem involving greedy reasoning and bit manipulation. Variants of similar seat allocation or interval blocking problems have appeared in interviews at large tech companies such as Amazon and Google.
What data structure is used in Cinema Seat Allocation?
The typical implementation uses a hash table (map) to group reserved seats by row. Many optimized solutions also use bitmasks to represent seat occupancy and perform constant-time checks with bitwise operations.
What is the time complexity of Cinema Seat Allocation?
The optimal solutions run in O(r) time and O(r) space, where r is the number of reserved seats or rows containing reservations. Only rows that appear in the reservation list require processing, while all other rows contribute two families automatically.

Ready to solve this problem?

Practice Cinema Seat Allocation with our built-in code editor and test cases.

Practice on FleetCode