FastPrepPoint Reachability Below a Strict Distance Threshold

Point Reachability Below a Strict Distance Threshold

Google logoGoogle● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given unordered points in the plane, an integer radius r, and the indices start and end.

Two different point indices i and j are directly connected exactly when their distance is strictly less than r. Connections work in both directions, and a route may use intermediate points.

For this exercise, assume points are immutable two-dimensional integer coordinates, distance is Euclidean, and both endpoints are indices into this collection. Equal coordinates at different indices remain different vertices.

Equivalently, the direct connection test is (points[i][0] - points[j][0])^2 + (points[i][1] - points[j][1])^2 < r^2. A distance equal to r is excluded.

Return true if a route from start to end exists, and false otherwise. For this exercise, assume a point reaches itself through a path of length zero, even when r == 0.

For this exercise, assume one reachability query over one immutable point set. Return only this Boolean; distance caching is a discussion follow-up, with no additional returned values or required spatial partitioning.

What the interview report shared

The report supplies a distance function, uses the strict rule distance < r, and describes graph construction with BFS or DFS. A large-data follow-up discusses caching or precomputing distances to avoid repeated calculations.

Function

canReachPoint(points: int[][], r: int, start: int, end: int) → boolean

Examples

Example 1

points = [[0,0],[2,0],[4,0],[20,0]]r = 3start = 0end = 2return = true

The route 0 -> 1 -> 2 uses two distances of 2, both below 3. The endpoints do not need a direct connection.

Example 2

points = [[0,0],[3,4]]r = 5start = 0end = 1return = false

The only possible connection has distance exactly 5, so the strict threshold excludes it.

Example 3

points = [[7,-2]]r = 0start = 0end = 0return = true

The endpoint is already the starting point. The length-zero path exists even with radius 0.

Constraints

  • For this exercise, assume 1 <= points.length <= 2000.
  • Every point contains exactly two integers; for this exercise, assume -10^9 <= x, y <= 10^9.
  • For this exercise, assume an integer radius 0 <= r <= 10^9 and valid indices 0 <= start, end < points.length.
  • Points may be in any order and may contain duplicate coordinates. The point collection does not change.

More Google problems

See Google hiring insights
public boolean canReachPoint(int[][] points, int r, int start, int end) {
    // Write your code here.
}
points[[0,0],[2,0],[4,0],[20,0]]
r3
start0
end2
expectedtrue
Checking account…