Place a Service Table Near All Customers
Updated
Problem statement
A rectangular restaurant grid contains 0 for an empty cell, 1 for a customer table, and -1 for a wall.
Choose one empty cell for a service table. Movement is allowed one cell up, down, left, or right and cannot pass through walls. Minimize the sum of shortest-path distances from the service table to every customer table.
Return the chosen coordinate [row,column]. Break ties by smaller row, then smaller column. Return [-1,-1] if there is no customer table or no empty cell can reach every customer table.
Function
bestServiceTable(grid: int[][]) → int[]Examples
Example 1
grid = [[1,0,0],[0,-1,0],[0,0,1]]return = [0,1]The cells [0,1] and [1,0] both have total distance four; row-major tie-breaking chooses [0,1].
Example 2
grid = [[1,-1,0],[0,-1,1],[0,0,0]]return = [1,0]Among cells reaching both customers, [1,0] minimizes the total distance.
Example 3
grid = [[1,-1,0],[-1,-1,1],[0,0,0]]return = [-1,-1]No empty cell can reach both customers.
Constraints
1 <= rows, columns <= 200.- Every cell is
-1,0, or1. - The grid contains at least one customer table.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Shortest Path in a Blocked GridSpotify · PHONE SCREEN
- Shortest Path in a Binary MatrixMeta · PHONE SCREENMeta interview questions
- Shortest Grid Path With One Wall BreakAmazon · PHONE SCREEN
- Shortest Path to Food with DirectionsZip · PHONE SCREENZip interview questions
- Shortest Path in a Grid with Obstacles EliminationMercor · PHONE SCREENMercor interview questions
- Shortest Path in a Grid with Obstacles EliminationBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Minimum MovesHarness · OA
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions