Count islands formed by land coordinates on an infinite grid

Quick Overview

Count the islands formed by a list of land cells on an infinite 2D grid, where two cells connect only when they share an edge. Coordinates reach one billion in either direction, so the question tests graph traversal on a sparse grid that is far too large to store as a matrix.

Count islands formed by land coordinates on an infinite grid

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of cell `coordinates` on an infinite 2D grid. Each coordinate `[x, y]` represents a land cell. Two land cells are considered connected if they are horizontally or vertically adjacent (sharing an edge). Return the number of islands. An island is a maximal group of connected land cells. Since the grid is infinite and coordinates can be very large (positive or negative), you cannot use a traditional 2D matrix to represent the grid. You must handle coordinate-based traversal efficiently. ### Function Signature ```python def count_islands(coordinates: list[list[int]]) -> int: ``` ### Rules - `coordinates[i] = [x, y]` is one land cell. Every cell that is not in the list is water. - Cells `[x1, y1]` and `[x2, y2]` are adjacent when `|x1 - x2| + |y1 - y2| == 1`. Cells that touch only at a corner are not adjacent. - Two land cells belong to the same island when a path of adjacent land cells joins them. - Return the island count as an integer. ### Constraints - `1 <= coordinates.length <= 10⁵` - `-10⁹ <= coordinates[i][0], coordinates[i][1] <= 10⁹` - All coordinates are unique. ### Examples ```text Input: coordinates = [[0,0],[0,1],[1,0],[1,1],[3,3],[3,4]] Output: 2 ``` The first four cells form a 2 by 2 block, which is one island. `[3,3]` and `[3,4]` differ by 1 in the second coordinate, so they form a second island. ```text Input: coordinates = [[1000000000,1000000000],[-1000000000,-1000000000],[0,0]] Output: 3 ``` All three cells are far apart and no two of them are adjacent, so each cell is its own island. ```text Input: coordinates = [[-1,0],[0,-1],[0,0],[5,5],[6,6]] Output: 3 ``` `[-1,0]` and `[0,-1]` both share an edge with `[0,0]`, so those three cells are one island. `[5,5]` and `[6,6]` touch only at a corner, so each of them is a separate island.

Overview: Count the islands formed by a list of land cells on an infinite 2D grid, where two cells connect only when they share an edge. Coordinates reach one billion in either direction, so the question tests graph traversal on a sparse grid that is far too large to store as a matrix.

Google logo
Google
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

You are given a list of cell coordinates on an infinite 2D grid. Each coordinate [x, y] represents a land cell. Two land cells are considered connected if they are horizontally or vertically adjacent (sharing an edge).

Return the number of islands. An island is a maximal group of connected land cells.

Since the grid is infinite and coordinates can be very large (positive or negative), you cannot use a traditional 2D matrix to represent the grid. You must handle coordinate-based traversal efficiently.

Function Signature

def count_islands(coordinates: list[list[int]]) -> int:

Rules

  • coordinates[i] = [x, y] is one land cell. Every cell that is not in the list is water.
  • Cells [x1, y1] and [x2, y2] are adjacent when |x1 - x2| + |y1 - y2| == 1 . Cells that touch only at a corner are not adjacent.
  • Two land cells belong to the same island when a path of adjacent land cells joins them.
  • Return the island count as an integer.

Constraints

  • 1 <= coordinates.length <= 10⁵
  • -10⁹ <= coordinates[i][0], coordinates[i][1] <= 10⁹
  • All coordinates are unique.

Examples

Input: coordinates = [[0,0],[0,1],[1,0],[1,1],[3,3],[3,4]]
Output: 2

The first four cells form a 2 by 2 block, which is one island. [3,3] and [3,4] differ by 1 in the second coordinate, so they form a second island.

Input: coordinates = [[1000000000,1000000000],[-1000000000,-1000000000],[0,0]]
Output: 3

All three cells are far apart and no two of them are adjacent, so each cell is its own island.

Input: coordinates = [[-1,0],[0,-1],[0,0],[5,5],[6,6]]
Output: 3

[-1,0] and [0,-1] both share an edge with [0,0], so those three cells are one island. [5,5] and [6,6] touch only at a corner, so each of them is a separate island.