Distance From Every Desk to the Nearest Exit on an Office Floor
Last updated: November 29, 2026
Quick Overview
An office floor plan is a grid where 'E' is an exit, '#' is a wall, and '.' is a desk or open floor. Return a grid of the walking distance from each open cell to its nearest exit, using -1 for walls and unreachable cells.
Meta
November 29, 20260
0
0 solved
An office floor plan is a grid where 'E' is an exit, '#' is a wall, and '.' is a desk or open floor. Return a grid of the walking distance from each open cell to its nearest exit, using -1 for walls and unreachable cells.
Multi-source BFS in the style of a Meta technical screen, framed around evacuation planning for a large open-plan campus building. The interviewer checks that you seed BFS with all exits at once rather than running one search per desk or per exit.
What the Interviewer Expects
- Rejects a BFS per desk as O((RC)^2)
- Seeds a single BFS queue with every exit at distance 0
- Explains why one BFS from all sources gives the nearest distance
- Marks walls and unreachable cells as -1
- Uses a distance array as the visited set
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you also return which exit each desk should use?
- What if some corridors are slower and take two units?
- How would you update distances when one exit is closed?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of strings or char grid with 'E', '#', '.'. Output: integer grid of distances, 0 at exits, -1 for walls and for open cells with no path to any exit. Movement is 4-directional. Grids up to ...
High-Level Design
Add every exit to the queue at distance 0. Run ordinary BFS. Because the queue is processed in nondecreasing distance order, the first time a cell is reached it is reached from its closest exit. This ...