Seat the most four-person groups in cinema rows with reserved seats

Quick Overview

Given the number of rows in a cinema and a list of reserved seats, count the maximum number of four-person groups that fit in the allowed seat blocks of each row. It tests per-row case analysis with overlapping blocks, compact seat lookups such as bitmasks, and handling a row count far too large to loop over.

Seat the most four-person groups in cinema rows with reserved seats

Company: Zomato

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A cinema has `n` rows of seats, numbered from 1 to `n`, and each row has ten seats labeled from 1 to 10. You are given `reserved_seats`, where `reserved_seats[i] = [r, s]` means seat `s` in row `r` is already reserved. Return the maximum number of four-person groups that can be seated. A group needs four adjacent seats in one row, and the aisles leave exactly three blocks that qualify: seats 2-5, seats 4-7 (which spans the middle of the row with two people on each side) and seats 6-9. ### Function Signature ```python def max_four_person_groups(n: int, reserved_seats: list[list[int]]) -> int: ``` ### Rules - A group must take all four seats of one of the blocks 2-5, 4-7 or 6-9 in a single row, and none of those seats may be reserved. - Groups cannot share seats, so two groups in the same row must use blocks that do not overlap. - Seats 1 and 10 belong to no block. ### Constraints - `1 <= n <= 10^9` - `1 <= len(reserved_seats) <= min(10 * n, 10^4)` - `len(reserved_seats[i]) == 2` - `1 <= reserved_seats[i][0] <= n` - `1 <= reserved_seats[i][1] <= 10` - All entries of `reserved_seats` are distinct. ### Examples ```text Input: n = 3, reserved_seats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]] Output: 4 ``` Row 1 has seats 2, 3 and 8 taken, so only the middle block 4-7 is free, and row 2 has seat 6 taken, which leaves block 2-5. Row 3 has only seats 1 and 10 taken, so blocks 2-5 and 6-9 both fit, for a total of 1 + 1 + 2 = 4. ```text Input: n = 2, reserved_seats = [[2,1],[1,8],[2,6]] Output: 2 ``` In row 1, seat 8 blocks 6-9, and the free blocks 2-5 and 4-7 overlap, so one group fits. In row 2, seat 6 blocks 4-7 and 6-9 while seat 1 does not matter, so block 2-5 holds one group. ```text Input: n = 4, reserved_seats = [[1,4],[1,7],[3,1],[3,10],[3,5]] Output: 5 ``` Seats 4 and 7 touch all three blocks, so row 1 seats nobody, and seat 5 removes blocks 2-5 and 4-7 from row 3 but leaves 6-9. Rows 2 and 4 have no reservations and hold two groups each, so the total is 0 + 2 + 1 + 2 = 5.

Overview: Given the number of rows in a cinema and a list of reserved seats, count the maximum number of four-person groups that fit in the allowed seat blocks of each row. It tests per-row case analysis with overlapping blocks, compact seat lookups such as bitmasks, and handling a row count far too large to loop over.

Zomato logo
Zomato
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A cinema has n rows of seats, numbered from 1 to n, and each row has ten seats labeled from 1 to 10. You are given reserved_seats, where reserved_seats[i] = [r, s] means seat s in row r is already reserved. Return the maximum number of four-person groups that can be seated.

A group needs four adjacent seats in one row, and the aisles leave exactly three blocks that qualify: seats 2-5, seats 4-7 (which spans the middle of the row with two people on each side) and seats 6-9.

Function Signature

def max_four_person_groups(n: int, reserved_seats: list[list[int]]) -> int:

Rules

  • A group must take all four seats of one of the blocks 2-5, 4-7 or 6-9 in a single row, and none of those seats may be reserved.
  • Groups cannot share seats, so two groups in the same row must use blocks that do not overlap.
  • Seats 1 and 10 belong to no block.

Constraints

  • 1 <= n <= 10^9
  • 1 <= len(reserved_seats) <= min(10 * n, 10^4)
  • len(reserved_seats[i]) == 2
  • 1 <= reserved_seats[i][0] <= n
  • 1 <= reserved_seats[i][1] <= 10
  • All entries of reserved_seats are distinct.

Examples

Input: n = 3, reserved_seats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
Output: 4

Row 1 has seats 2, 3 and 8 taken, so only the middle block 4-7 is free, and row 2 has seat 6 taken, which leaves block 2-5. Row 3 has only seats 1 and 10 taken, so blocks 2-5 and 6-9 both fit, for a total of 1 + 1 + 2 = 4.

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

In row 1, seat 8 blocks 6-9, and the free blocks 2-5 and 4-7 overlap, so one group fits. In row 2, seat 6 blocks 4-7 and 6-9 while seat 1 does not matter, so block 2-5 holds one group.

Input: n = 4, reserved_seats = [[1,4],[1,7],[3,1],[3,10],[3,5]]
Output: 5

Seats 4 and 7 touch all three blocks, so row 1 seats nobody, and seat 5 removes blocks 2-5 and 4-7 from row 3 but leaves 6-9. Rows 2 and 4 have no reservations and hold two groups each, so the total is 0 + 2 + 1 + 2 = 5.