Find grid cell minimizing sum distances
Applied IntuitionSoftware Engineer · Technical ScreenLast updated
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.
You are given an m x n 2D grid with k marked points. Movement is allowed in four directions (up, down, left, right) with unit cost per step. Find a grid cell whose sum of shortest-path distances to all marked points is minimized; if any marked point is unreachable from every cell, return -1. Implement a brute-force BFS-based solution that computes these distances, return both the minimum total distance and one optimal cell, and analyze time and space complexity. Discuss when multi-source BFS or using coordinate medians (if there are no obstacles) can reduce complexity.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.