Can you reach target with distance-threshold edges?
Company: Google
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph connectivity and spatial proximity modeling, examining competencies in graph algorithms and computational geometry within the Coding & Algorithms domain relevant for Machine Learning Engineer roles.
Part 1: Reachability in a Distance-Threshold Point Graph
Constraints
- 1 <= len(points) <= 2000
- points contains distinct 2D points
- -10^9 <= x, y <= 10^9
- 0 <= r <= 10^9
- start and end are both present in points
Examples
Input: ([(0, 0), (1, 0), (2, 0), (4, 0)], (0, 0), (2, 0), 1.5)
Expected Output: True
Explanation: (0,0) connects to (1,0), and (1,0) connects to (2,0), so a path exists.
Input: ([(0, 0), (1, 0), (2, 0), (4, 0)], (0, 0), (4, 0), 1.5)
Expected Output: False
Explanation: The first three points form a chain, but (4,0) is too far from (2,0) to connect.
Hints
- Think of the points as nodes in an implicit graph. You do not need to store every edge ahead of time.
- To test whether distance < r, compare squared distance with r * r and avoid calling sqrt.
Part 2: Scalable Reachability with Spatial Bucketing
Constraints
- 1 <= len(points) <= 200000
- points contains distinct 2D integer points
- -10^9 <= x, y <= 10^9
- 0 <= start_index, end_index < len(points)
- 1 <= r <= 10^9
- Hidden tests guarantee that every square bucket of side length r contains at most 25 points
Examples
Input: ([(0, 0), (2, 0), (4, 1), (6, 1)], 3, 0, 3)
Expected Output: True
Explanation: Point 0 connects to 1, 1 connects to 2, and 2 connects to 3. So there is a path from index 0 to index 3.
Input: ([(0, 0), (3, 0), (6, 0)], 3, 0, 2)
Expected Output: False
Explanation: Distances between consecutive points are exactly 3, but edges require distance to be strictly less than 3, so no edges exist.
Hints
- If two points are closer than r, then their x-coordinates differ by less than r and their y-coordinates differ by less than r. That means they must lie in the same bucket or one of the 8 neighboring buckets.
- Process points bucket by bucket and use Union-Find (Disjoint Set Union) so each candidate pair is checked at most once.