PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Compute distance to nearest taxi in grid

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of graph traversal and shortest-path reasoning on grids, algorithmic problem-solving for computing distances from many targets, and the ability to manage time and space complexity on large inputs.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Compute distance to nearest taxi in grid

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `R x C` grid representing a city map. - `grid[r][c] = 1` means there is a taxi located at cell `(r, c)`. - `grid[r][c] = 0` means the cell is empty. - You can move from a cell to its 4-directional neighbors (up/down/left/right). Each move costs 1. Return an `R x C` matrix `dist` where `dist[r][c]` is the length of the shortest path from cell `(r, c)` to **any** taxi. If a cell cannot reach any taxi, set its distance to `-1`. ### Input - `grid`: a 2D array of 0/1 integers ### Output - `dist`: a 2D array of integers ### Constraints (typical interview bounds) - `1 <= R, C <= 2000` (or any size where an `O(R*C)` solution is expected) - There may be `0` taxis or many taxis. ### Follow-ups 1. Is DFS an appropriate alternative here if we want shortest distances? Why or why not? 2. What changes if movement is allowed in 8 directions instead of 4?

Quick Answer: This question evaluates understanding of graph traversal and shortest-path reasoning on grids, algorithmic problem-solving for computing distances from many targets, and the ability to manage time and space complexity on large inputs.

Related Interview Questions

  • Find Containing Range - Google (medium)
  • Rearrange Tasks With Cooldown - Google (medium)
  • Solve Three Array and Matrix Path Problems - Google (medium)
  • Consolidate On-Call Rotation Segments - Google (medium)
  • Solve Flower Placement and Directory Deletion - Google (medium)
Google logo
Google
Jan 10, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
23
0
Loading...

You are given an R x C grid representing a city map.

  • grid[r][c] = 1 means there is a taxi located at cell (r, c) .
  • grid[r][c] = 0 means the cell is empty.
  • You can move from a cell to its 4-directional neighbors (up/down/left/right). Each move costs 1.

Return an R x C matrix dist where dist[r][c] is the length of the shortest path from cell (r, c) to any taxi.

If a cell cannot reach any taxi, set its distance to -1.

Input

  • grid : a 2D array of 0/1 integers

Output

  • dist : a 2D array of integers

Constraints (typical interview bounds)

  • 1 <= R, C <= 2000 (or any size where an O(R*C) solution is expected)
  • There may be 0 taxis or many taxis.

Follow-ups

  1. Is DFS an appropriate alternative here if we want shortest distances? Why or why not?
  2. What changes if movement is allowed in 8 directions instead of 4?

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.