PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates grid-based graph traversal and algorithmic problem-solving skills, including concepts such as multi-source breadth-first search, handling sentinel values, and performing in-place updates.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Fill rooms with nearest gate distances

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

You are given an m×n integer grid representing rooms, where -1 is a wall, 0 is a gate, and INF (a large sentinel such as 2^31− 1) is an empty room. For each empty room, fill it with the minimum number of steps to the nearest gate using four-directional movement; if unreachable, leave it as INF. Implement an in-place algorithm, justify your traversal strategy (e.g., multi-source BFS), and analyze time and space complexity. Cover edge cases such as multiple gates, enclosed areas, and very large grids.

Quick Answer: This question evaluates grid-based graph traversal and algorithmic problem-solving skills, including concepts such as multi-source breadth-first search, handling sentinel values, and performing in-place updates.

Given rooms grid with -1 walls, 0 gates, and INF empty rooms, fill each reachable room with distance to nearest gate.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

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: Classic walls and gates.

Input: ([],)

Expected Output: []

Explanation: Empty grid.

Hints

  1. Model object-style prompts as arrays or operation streams when needed.
  2. Handle empty and boundary cases before the main logic.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • AI Coding 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

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)