Group points by distance threshold
Problem Description
You are given N points in a 2D plane, where each point i is represented by its coordinates (xi, yi). You are also provided with a distance threshold k, which is a positive number.
An undirected graph is defined where an edge exists between any two points i and j if their Euclidean distance is strictly less than k.
A "group" is defined as a connected component within this graph. This implies transitivity: if point A is within distance k of point B, and point B is within distance k of point C, then A, B, and C are all considered to be in the same group, even if the direct distance between A and C is not strictly less than k.
Task
Your task is to return the grouping of points. This can be expressed as either the total number of distinct groups formed, or a list of point indices belonging to each group.
Follow-up
Consider a scenario where N is very large, and the points are very sparse (meaning most pairs of points are far apart). How would you significantly reduce the number of distance computations compared to a naive approach that checks all O(N^2) pairs?
Assumptions and Constraints
1 <= N <= 2e5- Coordinates are integers within a large range, for example,
[-1e9, 1e9]. - The distance metric to be used is Euclidean distance:
dist((x1,y1),(x2,y2)) = sqrt((x1-x2)^2 + (y1-y2)^2).
AI interview prep
Core dilemma
Balancing the computational cost of finding all relevant point pairs (which can be O(N^2) naively) with the need to correctly identify all connected components in a potentially very large graph, especially under the 'sparse points, large N' follow-up constraint.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Group points by distance threshold
Problem Description
You are given N points in a 2D plane, where each point i is represented by its coordinates (xi, yi). You are also provided with a distance threshold k, which is a positive number.
An undirected graph is defined where an edge exists between any two points i and j if their Euclidean distance is strictly less than k.
A "group" is defined as a connected component within this graph. This implies transitivity: if point A is within distance k of point B, and point B is within distance k of point C, then A, B, and C are all considered to be in the same group, even if the direct distance between A and C is not strictly less than k.
Task
Your task is to return the grouping of points. This can be expressed as either the total number of distinct groups formed, or a list of point indices belonging to each group.
Follow-up
Consider a scenario where N is very large, and the points are very sparse (meaning most pairs of points are far apart). How would you significantly reduce the number of distance computations compared to a naive approach that checks all O(N^2) pairs?
Assumptions and Constraints
1 <= N <= 2e5- Coordinates are integers within a large range, for example,
[-1e9, 1e9]. - The distance metric to be used is Euclidean distance:
dist((x1,y1),(x2,y2)) = sqrt((x1-x2)^2 + (y1-y2)^2).