FastPrepDistance to the Nearest Taxi in a Grid
Problems/Distance to the Nearest Taxi in a Grid

Distance to the Nearest Taxi in a Grid

Google logoGoogle● MediumNEW GRADPHONE SCREEN

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The firsthand Google new-grad report describes a Rotting Oranges-like matrix with several taxis and asks for every cell's shortest distance to any taxi. The interviewer then asks how to maintain distances as taxis randomly come online or go offline; that dynamic-system extension is discussed in the editorial but is not part of this batch return value.

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 200000 cells.

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

Interview reports that mention this question

  • Google New Grad R1 Nearest Taxi BFS and BQ: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[][] nearestTaxiDistances(String[] city) {
        // Write your code here.
    }
}
Checking account…