Distance to the Nearest Taxi in a Grid
Updated
Problem statement
A rectangular city grid contains taxis marked 'T' and ordinary cells marked '.'. Movement between cells is allowed up, down, left, and right.
Return a matrix where each cell contains its minimum number of moves to any taxi. A taxi cell has distance zero.
Function
nearestTaxiDistances(city: String[]) → int[][]Examples
Example 1
city = ["T..","...","..T"]return = [[0,1,2],[1,2,1],[2,1,0]]Distances expand from both taxis at once.
Example 2
city = [".T."]return = [[1,0,1]]The one-row grid is symmetric around the taxi.
Example 3
city = ["T.",".."]return = [[0,1],[1,2]]Each move changes Manhattan distance by one.
Constraints
1 <= city.length, city[i].length <= 1000.- Every row has the same length.
- Every character is
'T'or'.'. - At least one taxi is present.
- The grid has at most
200000cells.
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
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
- Count Connected IslandsUber · PHONE SCREENUber interview questions
- Number of IslandsSpaceX · PHONE SCREENSpaceX interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Xiangqi Horse ReachabilityeBay · PHONE SCREENeBay interview questions
- Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions