Find the minimum area of an axis-aligned rectangle formed by four given points

Quick Overview

Given up to 500 distinct points on a grid, find the smallest area of a rectangle with sides parallel to the axes whose four corners are all input points, returning 0 when none exists. The problem tests hashing, pair enumeration and complexity analysis.

Find the minimum area of an axis-aligned rectangle formed by four given points

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an array `points` of points in the X-Y plane, where `points[i] = [x_i, y_i]`. Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is no such rectangle, return `0`. ### Function Signature ```python def min_area_rect(points: list[list[int]]) -> int: ``` ### Rules - All four corners of the rectangle must be points from `points`. - The rectangle must have positive area, so its corners use two different x-coordinates and two different y-coordinates. - Other input points may lie inside the rectangle or on its edges. ### Constraints - `1 <= points.length <= 500` - `points[i].length == 2` - `0 <= x_i, y_i <= 4 * 10^4` - All points are distinct. ### Examples ```text Input: points = [[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]] Output: 4 ``` The corners (1, 1), (1, 3), (3, 1) and (3, 3) form a 2 by 2 square with area 4. The point (2, 2) sits inside it, and no other rectangle can be formed. ```text Input: points = [[1, 1], [1, 3], [3, 1], [3, 3], [4, 1], [4, 3]] Output: 2 ``` The y-values 1 and 3 both appear at x = 1, 3 and 4, so each pair of those x-values forms a rectangle. The areas are 4 (x = 1 and 3), 6 (x = 1 and 4) and 2 (x = 3 and 4), and the smallest is 2. ```text Input: points = [[1, 1], [1, 3], [3, 1]] Output: 0 ``` Three corners of a rectangle are present, but (3, 3) is missing, so no rectangle can be formed.

Overview: Given up to 500 distinct points on a grid, find the smallest area of a rectangle with sides parallel to the axes whose four corners are all input points, returning 0 when none exists. The problem tests hashing, pair enumeration and complexity analysis.

Google logo
Google
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an array points of points in the X-Y plane, where points[i] = [x_i, y_i].

Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is no such rectangle, return 0.

Function Signature

def min_area_rect(points: list[list[int]]) -> int:

Rules

  • All four corners of the rectangle must be points from points .
  • The rectangle must have positive area, so its corners use two different x-coordinates and two different y-coordinates.
  • Other input points may lie inside the rectangle or on its edges.

Constraints

  • 1 <= points.length <= 500
  • points[i].length == 2
  • 0 <= x_i, y_i <= 4 * 10^4
  • All points are distinct.

Examples

Input: points = [[1, 1], [1, 3], [3, 1], [3, 3], [2, 2]]
Output: 4

The corners (1, 1), (1, 3), (3, 1) and (3, 3) form a 2 by 2 square with area 4. The point (2, 2) sits inside it, and no other rectangle can be formed.

Input: points = [[1, 1], [1, 3], [3, 1], [3, 3], [4, 1], [4, 3]]
Output: 2

The y-values 1 and 3 both appear at x = 1, 3 and 4, so each pair of those x-values forms a rectangle. The areas are 4 (x = 1 and 3), 6 (x = 1 and 4) and 2 (x = 3 and 4), and the smallest is 2.

Input: points = [[1, 1], [1, 3], [3, 1]]
Output: 0

Three corners of a rectangle are present, but (3, 3) is missing, so no rectangle can be formed.