Amazon Phone Interview SDE2
Anonymous User
2854

Q. You're given a 2d matrix, where '1' means an amazon sorting center, and 0 means a normal road. Amazon needs to open a new sorting center, and its must be as far as it can from other sorting centers.

Write a function to return the max distance from all sorting centers. Here, distance means Manhattan distance, i.e. distance(p1, p2) = |x1 - x2| + |y1 - y2| where p is a point (x, y).

Example
Input:

1 0 1
0 0 0 
1 0 1

Output:

2

Exaplaination - (1, 1) is at distance 2 from all sorting centers.

Example 2
Input

1 0 0 
0 0 0 
0 0 0 

Output: 4

Explaination: (2,2) is farthest from (0,0) and distance is |2-0| + |2-0| = 4

I gave 2 approaches for this questions, but was not able to code the most optimal approach in given time. Can others share their approaches and solutions?

Comments (8)