This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Try to solve it in one dimension first. How can this solution apply to the two dimension case?
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of this problem appear in interviews at companies like Google, Amazon, and Meta. It tests understanding of Manhattan distance, medians, and grid-based problem optimization.
The optimal approach uses the median of the row and column positions of all people on the grid. Because Manhattan distance is minimized at the median, selecting the median row and column gives the minimal total travel distance.
Simple arrays or lists are typically used to store the row and column indices of homes. After collecting them, sorting or using two pointers helps efficiently compute the median-based distance.
For Manhattan distance, the total absolute distance is minimized when the meeting coordinate is the median of all coordinates. This property allows the problem to be solved independently for rows and columns.