Find possible robot positions
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to reason about grid traversal, boundary conditions, directional visibility counting, and constraint-based filtering to identify feasible positions.
Constraints
- 0 <= len(grid) <= 500
- If grid is non-empty, 0 <= len(grid[i]) <= 500 for every row i
- All rows in grid have the same length
- grid[i][j] is either '.' or '#'
- 0 <= up, down, left, right <= 500
Examples
Input: (['...', '.#.', '...'], 1, 1, 0, 0)
Expected Output: [[1, 0], [1, 2]]
Explanation: Cells (1,0) and (1,2) each have one open cell above, one below, and walls or boundaries immediately to the left and right.
Input: (['....'], 0, 0, 1, 2)
Expected Output: [[0, 1]]
Explanation: In the single row, cell (0,1) has one open cell to its left and two open cells to its right, with no cells above or below.
Input: (['.'], 0, 0, 0, 0)
Expected Output: [[0, 0]]
Explanation: The only open cell has no open cells visible in any direction.
Input: (['###', '###'], 0, 0, 0, 0)
Expected Output: []
Explanation: There are no open cells where the robot could stand.
Input: (['..#..', '...#.', '#....', '..#..'], 2, 1, 0, 3)
Expected Output: [[2, 1]]
Explanation: Cell (2,1) has two open cells upward, one downward, zero to the left because of a wall, and three open cells to the right.
Input: ([], 0, 0, 0, 0)
Expected Output: []
Explanation: An empty grid has no possible robot positions.
Hints
- For any open cell, each sensor reading is the length of a contiguous run of '.' cells in one direction, excluding the cell itself.
- Precompute vertical counts for each cell. Horizontal counts can be found efficiently by scanning each row's contiguous open segments.