Solve grid path and robot navigation
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in graph traversal and pathfinding, state representation for unknown environments, exploration and backtracking, and rigorous reasoning about correctness and time/space complexity.
Shortest Path in a Grid with Obstacles
Constraints
- 1 <= m, n <= 1000
- grid[i][j] is either 0 (open) or 1 (obstacle)
- If grid[0][0] == 1 or grid[m-1][n-1] == 1, the answer is -1
- Path length is counted as the number of cells visited (start cell counts as 1)
Examples
Input: ([[0,0,0],[1,1,0],[0,0,0]],)
Expected Output: 5
Explanation: Go right twice along the top row to (0,2), down to (1,2), down to (2,2), left to (2,1): 5 cells visited.
Input: ([[0,1],[1,0]],)
Expected Output: -1
Explanation: The only neighbors of the start are obstacles, so the destination is unreachable.
Input: ([[0]],)
Expected Output: 1
Explanation: Start and destination are the same single open cell; the path visits 1 cell.
Input: ([[1]],)
Expected Output: -1
Explanation: The start cell is itself an obstacle, so there is no valid path.
Input: ([[0,0,0],[0,0,0],[0,0,0]],)
Expected Output: 5
Explanation: Manhattan distance from (0,0) to (2,2) is 4 moves = 5 cells visited, with no obstacles to detour around.
Input: ([[0,0],[0,1]],)
Expected Output: -1
Explanation: The bottom-right destination cell is an obstacle, so it is unreachable.
Input: ([],)
Expected Output: -1
Explanation: An empty grid has no path; return -1.
Input: ([[0,1,0],[0,1,0],[0,0,0]],)
Expected Output: 5
Explanation: The middle column is blocked, so go down the left column to (2,0), then right to (2,2): 5 cells.
Hints
- All moves cost the same, so BFS (not Dijkstra/DFS) gives the shortest path on the first arrival at the destination.
- Mark a cell visited the moment you enqueue it (not when you pop it) to avoid enqueuing the same cell multiple times.
- Handle the degenerate cases first: empty grid, a blocked start, or a blocked destination should all return -1. A 1x1 open grid returns 1.
Robot Search in an Unknown Room
Constraints
- The robot does not know the grid; it may only use move()/atTarget() semantics during exploration.
- Coordinates are tracked relative to the start cell, which is treated as (0, 0).
- Steps are counted as moves between adjacent cells (start == target yields 0).
- Return -1 if the target is unreachable, or if start/target is a wall or out of bounds.
- 1 <= m, n; grid[i][j] is 0 (open) or 1 (wall)
Examples
Input: ([[0,0,0],[1,1,0],[0,0,0]], [0,0], [2,0])
Expected Output: 6
Explanation: The middle row is mostly walls; the only route from (0,0) to (2,0) goes right across the top, down the right column, and left across the bottom: 6 steps.
Input: ([[0,0,0],[0,0,0],[0,0,0]], [0,0], [2,2])
Expected Output: 4
Explanation: An open 3x3 room: the Manhattan distance from (0,0) to (2,2) is 4 steps.
Input: ([[0,1],[1,0]], [0,0], [1,1])
Expected Output: -1
Explanation: The target (1,1) is isolated by walls; the robot can never reach it, so return -1.
Input: ([[0]], [0,0], [0,0])
Expected Output: 0
Explanation: Start and target are the same cell; the robot is already at the target with 0 steps.
Input: ([[0,0,0,0]], [0,0], [0,3])
Expected Output: 3
Explanation: A single open corridor of length 4; moving right three times reaches the target.
Input: ([[0,1,0],[0,1,0],[0,0,0]], [0,0], [0,2])
Expected Output: 6
Explanation: The middle column is walled off, so the robot must travel down the left column, across the bottom, and up the right column: 6 steps.
Input: ([[0,0],[0,0]], [0,0], [1,1])
Expected Output: 2
Explanation: Open 2x2 room; the diagonal corner is 2 steps away (down then right, or right then down).
Input: ([[0,0,0],[1,1,0],[0,0,0]], [0,0], [2,2])
Expected Output: 4
Explanation: Despite the walled middle row, (2,2) is reached via the right column: right, right, down, down = 4 steps.
Hints
- DFS alone cannot give the minimal step count — it only discovers which cells are reachable. Use DFS (or any flood fill) to MAP the room, then BFS over the discovered cells to MEASURE the shortest distance.
- Track position with relative offsets: treat the start as (0,0) and add the direction delta on each successful move. This lets you build a coordinate system without ever knowing the absolute grid.
- After exploring a branch, issue the opposite move to physically backtrack the robot to its previous cell, so the relative coordinate you track always matches where the robot actually stands.