FastPrepRouter Reachability Within Manhattan Range

Router Reachability Within Manhattan Range

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Each router is a point [x,y]. Two routers can communicate directly when their Manhattan distance is at most range.

Given start and end router indices, return whether data can be relayed from start to end through any sequence of direct links.

All links are bidirectional. A router can always reach itself.

Function

canRoute(routers: int[][], range: int, start: int, end: int) → boolean

Examples

Example 1

routers = [[0,0],[0,8],[10,8],[0,28]]range = 10start = 0end = 2return = true

Router 0 reaches 1, and router 1 reaches 2.

Example 2

routers = [[0,0],[20,0]]range = 10start = 0end = 1return = false

The only pair is outside range.

Example 3

routers = [[5,5]]range = 0start = 0end = 0return = true

A router reaches itself without a link.

Constraints

  • 1 <= routers.length <= 2000.
  • Each router contains two coordinates in [-10^9,10^9].
  • 0 <= range <= 2*10^9.
  • Start and end are valid indices.

More Google problems

See Google hiring insights
public boolean canRoute(int[][] routers, int range, int start, int end) {
    // Write your code here.
}
routers[[0,0],[0,8],[10,8],[0,28]]
range10
start0
end2
expectedtrue
Checking account…