Count Lattice Points in a Union of Circles

Quick Overview

Count the distinct integer grid points that lie inside or on the boundary of at least one circle in a list, where radii reach 100,000 and the answer can exceed 32 bits. It tests exact integer geometry, avoiding floating point errors at the circle boundary, and counting points shared by several circles only once.

Count Lattice Points in a Union of Circles

Company: Rubrik

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 2D integer array `circles` where `circles[i] = [xi, yi, ri]` represents the center `(xi, yi)` and the positive radius `ri` of the `i`-th circle drawn on a grid. Return the number of distinct lattice points that lie inside or on the circumference of at least one circle. A point covered by several circles is counted only once. A lattice point is a point with integer coordinates. Note that the answer may exceed the range of a 32-bit integer. ### Function Signature ```python def count_lattice_points(circles: list[list[int]]) -> int: ``` ### Rules - A lattice point `(x, y)` is covered by the circle `[cx, cy, r]` when `(x - cx)^2 + (y - cy)^2 <= r^2`, so points on the circumference count. - Each covered point counts once, however many circles cover it. ### Constraints - `1 <= len(circles) <= 1000` - `len(circles[i]) == 3` - `-10^5 <= xi, yi <= 10^5` - `1 <= ri <= 10^5` - The sum of `2 * ri + 1` over all circles is at most `2 * 10^5`. ### Examples ```text Input: circles = [[2, 2, 1]] Output: 5 ``` The lattice points inside or on the circle are (1, 2), (2, 1), (2, 2), (2, 3) and (3, 2). Diagonal neighbors such as (1, 1) are at distance $\sqrt{2} > 1$ from the center, so they are outside. ```text Input: circles = [[2, 2, 2], [3, 4, 1]] Output: 16 ``` The circle centered at (2, 2) covers 13 lattice points and the circle centered at (3, 4) covers 5. The two shared points are (2, 4) and (3, 3), so the union contains 13 + 5 - 2 = 16 distinct points.

Overview: Count the distinct integer grid points that lie inside or on the boundary of at least one circle in a list, where radii reach 100,000 and the answer can exceed 32 bits. It tests exact integer geometry, avoiding floating point errors at the circle boundary, and counting points shared by several circles only once.

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

You are given a 2D integer array circles where circles[i] = [xi, yi, ri] represents the center (xi, yi) and the positive radius ri of the i-th circle drawn on a grid.

Return the number of distinct lattice points that lie inside or on the circumference of at least one circle. A point covered by several circles is counted only once.

A lattice point is a point with integer coordinates. Note that the answer may exceed the range of a 32-bit integer.

Function Signature

def count_lattice_points(circles: list[list[int]]) -> int:

Rules

  • A lattice point (x, y) is covered by the circle [cx, cy, r] when (x - cx)^2 + (y - cy)^2 <= r^2 , so points on the circumference count.
  • Each covered point counts once, however many circles cover it.

Constraints

  • 1 <= len(circles) <= 1000
  • len(circles[i]) == 3
  • -10^5 <= xi, yi <= 10^5
  • 1 <= ri <= 10^5
  • The sum of 2 * ri + 1 over all circles is at most 2 * 10^5 .

Examples

Input: circles = [[2, 2, 1]]
Output: 5

The lattice points inside or on the circle are (1, 2), (2, 1), (2, 2), (2, 3) and (3, 2). Diagonal neighbors such as (1, 1) are at distance 2>1\sqrt{2} > 1 from the center, so they are outside.

Input: circles = [[2, 2, 2], [3, 4, 1]]
Output: 16

The circle centered at (2, 2) covers 13 lattice points and the circle centered at (3, 4) covers 5. The two shared points are (2, 4) and (3, 3), so the union contains 13 + 5 - 2 = 16 distinct points.