PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates grid traversal, distance propagation, obstacle handling, and in-place matrix update competencies within the Coding & Algorithms domain. It is commonly asked in technical interviews to assess algorithmic problem-solving and traversal strategies under obstacle and complexity constraints, representing a practical application-level problem rather than purely conceptual theory.

  • medium
  • Robinhood
  • Coding & Algorithms
  • Software Engineer

Compute nearest gate distance in grid

Company: Robinhood

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` grid representing a floor plan. - `0` = a gate - `-1` = a blocked cell (wall/obstacle); cannot be entered - `INF` = an empty cell (a large integer such as `2^31 - 1`) For every empty cell, fill it with the Manhattan distance (number of up/down/left/right steps) to its **nearest gate**. If an empty cell cannot reach any gate, leave it as `INF`. ### Requirements - Update the grid **in place**. - Movement is only in 4 directions. ### Example Input: ``` INF -1 0 INF INF INF INF -1 INF -1 INF -1 0 -1 INF INF ``` Output: ``` 3 -1 0 1 2 2 1 -1 1 -1 2 -1 0 -1 3 4 ``` ### Constraints (typical) - `1 <= m, n <= 200` - Grid values are only in `{ -1, 0, INF }`

Quick Answer: This question evaluates grid traversal, distance propagation, obstacle handling, and in-place matrix update competencies within the Coding & Algorithms domain. It is commonly asked in technical interviews to assess algorithmic problem-solving and traversal strategies under obstacle and complexity constraints, representing a practical application-level problem rather than purely conceptual theory.

You are given an `m x n` grid representing a floor plan. Each cell contains one of three values: - `0` for a gate - `-1` for a wall/blocked cell - `INF` for an empty cell, where `INF = 2147483647` For every empty cell, fill it with the Manhattan distance to its nearest gate using only 4-directional movement (up, down, left, right). If an empty cell cannot reach any gate, leave it as `INF`. You must update the grid in place. For testing purposes, return the updated grid as well.

Constraints

  • 1 <= m, n <= 200
  • Each grid value is one of {-1, 0, 2147483647}
  • Movement is allowed only in 4 directions: up, down, left, right

Examples

Input: ([[2147483647, -1, 0, 2147483647], [2147483647, 2147483647, 2147483647, -1], [2147483647, -1, 2147483647, -1], [0, -1, 2147483647, 2147483647]],)

Expected Output: [[3, -1, 0, 1], [2, 2, 1, -1], [1, -1, 2, -1], [0, -1, 3, 4]]

Explanation: Starting BFS from both gates fills each empty cell with the distance to the nearest gate.

Input: ([[0]],)

Expected Output: [[0]]

Explanation: A single gate already has distance 0.

Input: ([[2147483647, -1], [2147483647, 2147483647]],)

Expected Output: [[2147483647, -1], [2147483647, 2147483647]]

Explanation: There are no gates, so no empty cell can be updated.

Input: ([[0, 2147483647, 2147483647, 0, 2147483647]],)

Expected Output: [[0, 1, 1, 0, 1]]

Explanation: With multiple gates, each empty cell takes the distance to the closer gate.

Hints

  1. Running a separate BFS from every empty cell is too slow. Can you start from all gates at once instead?
  2. In an unweighted grid, the first time BFS reaches a cell is the shortest distance to that cell.
Last updated: May 4, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,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.

Related Coding Questions

  • Build a Weekly Calendar - Robinhood (medium)
  • Solve path and inventory problems - Robinhood
  • Implement Calendar Layout and String Packing - Robinhood (medium)
  • Aggregate user logs into 30-minute sessions - Robinhood (hard)
  • Count Referral Descendants - Robinhood (medium)