Count pairs of integer points that are exactly distance d apart

Quick Overview

Count the index pairs of integer points on a 2D plane whose Euclidean distance is exactly d, where the same point can appear more than once. It tests exact integer distance checks, hash map lookups, and avoiding a quadratic scan over all pairs.

Count pairs of integer points that are exactly distance d apart

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list `coordinates` where `coordinates[i] = [x_i, y_i]` is a point on a 2D plane, and a non-negative integer `d`. Return the number of index pairs `(i, j)` with `i < j` such that the Euclidean distance between `coordinates[i]` and `coordinates[j]` is exactly `d`. The Euclidean distance between `(x1, y1)` and `(x2, y2)` is $\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$. ### Function Signature ```python def count_pairs_at_distance(coordinates: list[list[int]], d: int) -> int: ``` ### Rules - All coordinates and `d` are integers, so a pair is valid exactly when `(x2 - x1)^2 + (y2 - y1)^2 == d^2`. Compare in integers, with no rounding. - Pairs are counted by index. The same point can appear several times in `coordinates`, and each copy is a separate point. - When `d` is 0, the valid pairs are the pairs of identical points. ### Constraints - `1 <= len(coordinates) <= 10^4` - `-10^4 <= x_i, y_i <= 10^4` - `0 <= d <= 2 * 10^4` ### Examples ```text Input: coordinates = [[0,0],[3,4],[0,5],[5,0],[3,0]], d = 5 Output: 3 ``` The point `(0,0)` is at distance 5 from `(3,4)` (9 + 16 = 25), from `(0,5)` and from `(5,0)`. No other pair works: for example `(3,4)` and `(0,5)` give 9 + 1 = 10, and `(0,5)` and `(5,0)` give 50. ```text Input: coordinates = [[0,0],[1,0],[0,1],[1,1]], d = 1 Output: 4 ``` The four sides of the unit square are at distance 1. The two diagonals have length $\sqrt{2}$ and do not count. ```text Input: coordinates = [[2,2],[2,2],[5,6],[2,2]], d = 5 Output: 3 ``` `(2,2)` appears at indices 0, 1 and 3, and each copy is at distance 5 from `(5,6)` (9 + 16 = 25), giving 3 pairs. The pairs of identical copies are at distance 0 and do not count.

Overview: Count the index pairs of integer points on a 2D plane whose Euclidean distance is exactly d, where the same point can appear more than once. It tests exact integer distance checks, hash map lookups, and avoiding a quadratic scan over all pairs.

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

You are given a list coordinates where coordinates[i] = [x_i, y_i] is a point on a 2D plane, and a non-negative integer d. Return the number of index pairs (i, j) with i < j such that the Euclidean distance between coordinates[i] and coordinates[j] is exactly d.

The Euclidean distance between (x1, y1) and (x2, y2) is (x2−x1)2+(y2−y1)2\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}.

Function Signature

def count_pairs_at_distance(coordinates: list[list[int]], d: int) -> int:

Rules

  • All coordinates and d are integers, so a pair is valid exactly when (x2 - x1)^2 + (y2 - y1)^2 == d^2 . Compare in integers, with no rounding.
  • Pairs are counted by index. The same point can appear several times in coordinates , and each copy is a separate point.
  • When d is 0, the valid pairs are the pairs of identical points.

Constraints

  • 1 <= len(coordinates) <= 10^4
  • -10^4 <= x_i, y_i <= 10^4
  • 0 <= d <= 2 * 10^4

Examples

Input: coordinates = [[0,0],[3,4],[0,5],[5,0],[3,0]], d = 5
Output: 3

The point (0,0) is at distance 5 from (3,4) (9 + 16 = 25), from (0,5) and from (5,0). No other pair works: for example (3,4) and (0,5) give 9 + 1 = 10, and (0,5) and (5,0) give 50.

Input: coordinates = [[0,0],[1,0],[0,1],[1,1]], d = 1
Output: 4

The four sides of the unit square are at distance 1. The two diagonals have length 2\sqrt{2} and do not count.

Input: coordinates = [[2,2],[2,2],[5,6],[2,2]], d = 5
Output: 3

(2,2) appears at indices 0, 1 and 3, and each copy is at distance 5 from (5,6) (9 + 16 = 25), giving 3 pairs. The pairs of identical copies are at distance 0 and do not count.