Find grid cell minimizing sum distances
Company: Applied Intuition
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in grid-based graph traversal and shortest-path computation using BFS, reachability analysis from multiple marked points, and algorithmic time/space complexity reasoning.
Constraints
- 1 <= m, n <= 30
- grid[i][j] is one of -1, 0, or 1
- There is at least one marked cell
- Movement is allowed only through cells whose value is not -1
Examples
Input: ([[1,0,0],[0,1,0],[0,0,1]],)
Expected Output: (4, (1, 1))
Explanation: Cell (1, 1) reaches the three marked cells in distances 2, 0, and 2, for a total of 4, which is minimal.
Input: ([[1,-1,0,0],[0,-1,0,1],[0,0,1,0]],)
Expected Output: (6, (2, 2))
Explanation: Obstacles force paths around the blocked cells; from (2, 2) the distances to the marks are 4, 2, and 0, summing to 6.
Input: ([[1,-1,1]],)
Expected Output: -1
Explanation: The obstacle splits the two marked cells into different connected components, so no traversable cell can reach both.
Input: ([[1]],)
Expected Output: (0, (0, 0))
Explanation: The only cell is already marked, so the minimum total distance is 0 at (0, 0).
Input: ([[1,0,1]],)
Expected Output: (2, (0, 0))
Explanation: All three traversable cells have total distance 2 to the two marks, so the lexicographically smallest optimal cell is (0, 0).
Hints
- Because every move has the same cost, shortest paths from one cell to all others in an unweighted grid are found with BFS.
- For the required brute-force solution, iterate over every non-blocked cell, run BFS, add distances when marked cells are first reached, and keep the best total. Scanning cells row by row naturally handles lexicographic tie-breaking.