Check whether axis-aligned rectangles exactly cover one rectangular region

Quick Overview

Decide whether a list of axis-aligned rectangles tiles one rectangular region exactly, with no gaps and no overlapping area. The problem tests geometric invariants, hashing of points, and catching cases where the total area matches but the cover is still wrong, all in linear time.

Check whether axis-aligned rectangles exactly cover one rectangular region

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given a list `rectangles` where `rectangles[i] = [x_i, y_i, a_i, b_i]` describes an axis-aligned rectangle. Its bottom-left corner is `(x_i, y_i)` and its top-right corner is `(a_i, b_i)`. Return `True` if the rectangles together form an exact cover of a rectangular region, and `False` otherwise. ### Function Signature ```python def is_rectangle_cover(rectangles: list[list[int]]) -> bool: ... ``` ### Rules - An exact cover means the union of all the rectangles is a single rectangle with no gaps, and no two rectangles overlap in a region of positive area. - Rectangles may touch along an edge or at a corner. Touching is not an overlap. ### Constraints - `1 <= len(rectangles) <= 2 * 10^4` - `len(rectangles[i]) == 4` - `-10^5 <= x_i, y_i, a_i, b_i <= 10^5` - `x_i < a_i` and `y_i < b_i`, so every rectangle has positive width and height ### Examples ```text Input: rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]] Output: True ``` The bounding region runs from (1, 1) to (4, 4) and has area 9. The square `[1,1,3,3]` fills the lower left, `[3,1,4,2]` and `[3,2,4,4]` fill the strip from x = 3 to x = 4, and `[1,3,2,4]` and `[2,3,3,4]` fill the top row above the square. The areas add up to 4 + 1 + 2 + 1 + 1 = 9 and nothing overlaps. ```text Input: rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]] Output: False ``` The bounding region from (1, 1) to (4, 4) has area 9, but the rectangles cover only 6. The vertical strip between x = 2 and x = 3 is empty. ```text Input: rectangles = [[0,0,2,1],[0,1,1,2],[0,1,1,2]] Output: False ``` The areas add up to 2 + 1 + 1 = 4, which equals the area of the 2 by 2 bounding region. Even so, `[0,1,1,2]` appears twice and the cell from (1, 1) to (2, 2) is not covered at all.

Overview: Decide whether a list of axis-aligned rectangles tiles one rectangular region exactly, with no gaps and no overlapping area. The problem tests geometric invariants, hashing of points, and catching cases where the total area matches but the cover is still wrong, all in linear time.

Apple logo
Apple
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list rectangles where rectangles[i] = [x_i, y_i, a_i, b_i] describes an axis-aligned rectangle. Its bottom-left corner is (x_i, y_i) and its top-right corner is (a_i, b_i).

Return True if the rectangles together form an exact cover of a rectangular region, and False otherwise.

Function Signature

def is_rectangle_cover(rectangles: list[list[int]]) -> bool:
    ...

Rules

  • An exact cover means the union of all the rectangles is a single rectangle with no gaps, and no two rectangles overlap in a region of positive area.
  • Rectangles may touch along an edge or at a corner. Touching is not an overlap.

Constraints

  • 1 <= len(rectangles) <= 2 * 10^4
  • len(rectangles[i]) == 4
  • -10^5 <= x_i, y_i, a_i, b_i <= 10^5
  • x_i < a_i and y_i < b_i , so every rectangle has positive width and height

Examples

Input: rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
Output: True

The bounding region runs from (1, 1) to (4, 4) and has area 9. The square [1,1,3,3] fills the lower left, [3,1,4,2] and [3,2,4,4] fill the strip from x = 3 to x = 4, and [1,3,2,4] and [2,3,3,4] fill the top row above the square. The areas add up to 4 + 1 + 2 + 1 + 1 = 9 and nothing overlaps.

Input: rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
Output: False

The bounding region from (1, 1) to (4, 4) has area 9, but the rectangles cover only 6. The vertical strip between x = 2 and x = 3 is empty.

Input: rectangles = [[0,0,2,1],[0,1,1,2],[0,1,1,2]]
Output: False

The areas add up to 2 + 1 + 1 = 4, which equals the area of the 2 by 2 bounding region. Even so, [0,1,1,2] appears twice and the cell from (1, 1) to (2, 2) is not covered at all.