Compute reachable cells for a cleaning robot
Company: Pinterest
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of graph traversal, state-space modeling, and scalability when computing reachable cells on a 2D grid under 8-direction movement and variants that allow limited obstacle removal or multiple agents.
Part 1: Reachable Free Cells with 8-Directional Moves
Constraints
- `1 <= m, n <= 2000`
- `grid[r][c]` is either `0` or `1`
- The start cell is within bounds in valid inputs
- Use an iterative approach that is safe for large grids
Examples
Input: ([[0, 1, 0], [0, 0, 1], [1, 0, 0]], (0, 0))
Expected Output: [(0, 0), (0, 2), (1, 0), (1, 1), (2, 1), (2, 2)]
Explanation: Diagonal moves let the robot pass through the connected free region.
Input: ([[0, 1, 1], [1, 1, 1], [1, 1, 0]], (0, 0))
Expected Output: [(0, 0)]
Explanation: All neighboring cells are blocked, so the start cell is the only reachable free cell.
Input: ([[0]], (0, 0))
Expected Output: [(0, 0)]
Explanation: Single-cell edge case.
Input: ([[0, 1], [1, 0]], (0, 0))
Expected Output: [(0, 0), (1, 1)]
Explanation: The robot can move diagonally from `(0, 0)` to `(1, 1)`.
Hints
- Model the grid as a graph where each free cell is a node connected to up to 8 neighboring free cells.
- Use BFS or DFS iteratively; recursion can overflow on large grids.
Part 2: Reachable Cells with At Most One Obstacle Clear
Constraints
- `1 <= m, n <= 2000`
- `grid[r][c]` is either `0` or `1`
- The start cell is free in valid inputs
- The algorithm should be safe for large grids
Examples
Input: ([[0, 1, 0], [1, 1, 0], [0, 0, 0]], (0, 0))
Expected Output: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
Explanation: By choosing different blocked cells to clear, every coordinate in the grid becomes reachable in some valid traversal.
Input: ([[0, 1, 1, 0]], (0, 0))
Expected Output: [(0, 0), (0, 1)]
Explanation: The robot can clear `(0, 1)`, but then it cannot pass through `(0, 2)` because only one obstacle may be cleared.
Input: ([[0]], (0, 0))
Expected Output: [(0, 0)]
Explanation: Single-cell edge case.
Input: ([[0, 0], [0, 0]], (1, 1))
Expected Output: [(0, 0), (0, 1), (1, 0), (1, 1)]
Explanation: No obstacle needs to be cleared; all cells are reachable.
Hints
- A plain visited set is not enough. The state must also track whether the one allowed obstacle clear has already been used.
- The same cell may need to be explored twice: once with the clear still available, and once after it has been used.
Part 3: Union of Reachable Cells from Two Robots
Constraints
- `1 <= m, n <= 2000`
- `grid[r][c]` is either `0` or `1`
- Both start cells are within bounds and free in valid inputs
- Use an iterative solution that works for large grids
Examples
Input: ([[0, 0, 1], [1, 0, 1], [0, 1, 0]], (0, 0), (2, 2))
Expected Output: [(0, 0), (0, 1), (1, 1), (2, 0), (2, 2)]
Explanation: The two starts lie in the same 8-connected free component, so the union is that entire component.
Input: ([[0, 0, 1, 0], [1, 1, 1, 1], [0, 0, 1, 0]], (0, 0), (2, 0))
Expected Output: [(0, 0), (0, 1), (2, 0), (2, 1)]
Explanation: The blocked middle row separates the two reachable regions.
Input: ([[0, 0], [0, 0]], (0, 0), (1, 1))
Expected Output: [(0, 0), (0, 1), (1, 0), (1, 1)]
Explanation: Both robots are in the same open grid, so every free cell is reachable.
Input: ([[0]], (0, 0), (0, 0))
Expected Output: [(0, 0)]
Explanation: Single-cell edge case; both robots start on the same cell.
Hints
- You can run one multi-source BFS by placing both start cells into the queue initially.
- If both robots can reach the same region, a shared visited structure avoids duplicate work.