Count Clusters of 2D Points Within a Radius
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question tests graph traversal skills, specifically identifying connected components among a set of 2D points using distance-based adjacency. It assesses the ability to model a geometric problem as a graph and apply DFS, BFS, or Union-Find, a common evaluation of algorithmic thinking in coding interviews. It also probes awareness of efficiency trade-offs when scaling to large point sets.
Constraints
- 1 <= n <= 10^5
- -10^9 <= x, y <= 10^9 (use 64-bit integers for squared distances to avoid overflow)
- 0 <= r <= 2 * 10^9
- Points may be duplicated; identical points have distance 0 and are always in the same cluster
- Compare squared distances (dx*dx + dy*dy <= r*r) to avoid floating-point square roots
Examples
Input: ([[0, 0], [1, 0], [10, 10]], 2)
Expected Output: 2
Explanation: [0,0] and [1,0] are within distance 2 and merge; [10,10] is isolated. Two clusters.
Input: ([[0, 0], [3, 0], [6, 0]], 3)
Expected Output: 1
Explanation: [0,0]-[3,0] and [3,0]-[6,0] are each exactly distance 3 apart, so by transitivity all three form one cluster even though [0,0] and [6,0] are distance 6 apart.
Input: ([[0, 0], [5, 5], [100, 100]], 1)
Expected Output: 3
Explanation: No two points are within distance 1, so each point is its own cluster.
Input: ([[5, 5]], 0)
Expected Output: 1
Explanation: A single point always forms exactly one cluster, regardless of r.
Input: ([], 5)
Expected Output: 0
Explanation: No points means no clusters.
Input: ([[2, 2], [2, 2], [9, 9]], 0)
Expected Output: 2
Explanation: The two identical points at [2,2] have distance 0 <= 0 and merge; [9,9] is separate. Even with r=0, duplicates cluster together.
Input: ([[0, 0], [4, 3], [8, 6], [8, 6]], 5)
Expected Output: 1
Explanation: [0,0]-[4,3] and [4,3]-[8,6] are each distance 5; the duplicate [8,6] adds nothing. All four points chain into one cluster.
Input: ([[-1000000000, -1000000000], [1000000000, 1000000000]], 2000000000)
Expected Output: 2
Explanation: The diagonal distance is 2e9 * sqrt(2) which is about 2.83e9 > r = 2e9, so the points do NOT connect. This stresses 64-bit squared-distance arithmetic (~8e18).
Input: ([[0, 0], [1, 1], [2, 2], [10, 10], [11, 11]], 2)
Expected Output: 2
Explanation: The chain [0,0]-[1,1]-[2,2] merges into one cluster and the chain [10,10]-[11,11] into another; the two groups are far apart. Two clusters.
Hints
- This is a connected-components problem. Model each point as a node and add an edge between any two points within distance r, then count the components with Union-Find (or BFS/DFS).
- Never take a square root. Compare (x1-x2)^2 + (y1-y2)^2 against r*r. With coordinates up to 1e9 and r up to 2e9, both sides can reach ~8e18, so use 64-bit (long / long long) arithmetic to avoid overflow.
- Only check each unordered pair once (j starts at i+1). Start the component count at n and decrement on each successful union, or at the end count the roots where find(i) == i.
- Duplicates and r = 0 fall out naturally: distance 0 <= r*r whenever r >= 0, so identical points always merge.