PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This Coding & Algorithms question evaluates algorithmic problem-solving with grid-based graph traversal and shortest-distance computation, assessing data-structure use and implementation-level efficiency for software engineers.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Fill rooms with nearest-gate distance

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given an m×n grid representing a building floor plan: - `-1` = wall/blocked cell - `0` = gate - A large positive number (e.g., `INF = 2^31-1`) = empty room Fill each empty room with the distance to its nearest gate (Manhattan distance: up/down/left/right). If a gate cannot be reached, leave the value as `INF`. Return the modified grid (or modify it in-place).

Quick Answer: This Coding & Algorithms question evaluates algorithmic problem-solving with grid-based graph traversal and shortest-distance computation, assessing data-structure use and implementation-level efficiency for software engineers.

You are given an m x n grid representing a building floor plan. Each cell contains one of the following values: - -1 for a wall or blocked cell - 0 for a gate - 2147483647 for an empty room Fill every empty room with the distance to its nearest gate, where distance is the minimum number of moves using only up, down, left, or right. Walls cannot be crossed. If an empty room cannot reach any gate, leave it as 2147483647. Return the modified grid.

Constraints

  • 0 <= m, n <= 250
  • Each cell is one of: -1, 0, or 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: Distances are filled from both gates simultaneously. Each room gets the shortest path length to the nearest gate.

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

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

Explanation: The middle column of walls blocks the left column completely, so those rooms remain 2147483647.

Input: ([],)

Expected Output: []

Explanation: An empty grid has nothing to update.

Input: ([[2147483647]],)

Expected Output: [[2147483647]]

Explanation: There is no gate, so the single empty room stays unchanged.

Hints

  1. Instead of running a search from every empty room, think about starting from every gate at the same time.
  2. A breadth-first search guarantees that the first time you reach a room, you found its shortest distance.
Last updated: Jun 5, 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

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)