Find robots matching obstacle-distance signature
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates spatial reasoning on 2D grids, the ability to compute distance signatures to nearest obstacles, and careful handling of boundary and edge-case conditions.
Constraints
- 1 <= R, C <= 2000
- grid[r][c] is one of 'O', 'E', or 'X'
- dist has length 4 and is ordered as [left, top, bottom, right]
- 0 <= dist[i] <= max(R, C)
- The boundary outside the grid is considered an obstacle
Examples
Input: ([['E','O','E'], ['O','O','O'], ['E','X','E']], [1, 1, 0, 1])
Expected Output: [(1, 1)]
Explanation: Robot (1,1) has one non-obstacle cell to the left, one above, zero below because of the obstacle at (2,1), and one to the right.
Input: ([['O']], [0, 0, 0, 0])
Expected Output: [(0, 0)]
Explanation: The only robot is surrounded by the grid boundary in all four directions, so all distances are 0.
Input: ([['X','O','X']], [0, 0, 0, 0])
Expected Output: [(0, 1)]
Explanation: The robot has obstacles immediately to its left and right, and the top and bottom boundaries are immediate obstacles.
Input: ([['O','O','O']], [0, 0, 0, 0])
Expected Output: []
Explanation: No robot has distance 0 in both horizontal directions because all three robots are in one non-obstacle segment.
Input: ([['E','E','E','X','E','E','E'], ['E','O','E','X','E','O','E'], ['E','E','E','X','E','E','E']], [1, 1, 1, 1])
Expected Output: [(1, 1), (1, 5)]
Explanation: Both robots are centered in separate 3 by 3 non-obstacle regions, so each has one non-obstacle cell in every direction before an obstacle or boundary.
Input: ([['X','E','O','E','X'], ['E','E','O','E','E'], ['X','E','O','E','X']], [1, 0, 2, 1])
Expected Output: [(0, 2)]
Explanation: Robot (0,2) has left/right distances 1, top distance 0 due to the boundary, and bottom distance 2 before the bottom boundary.
Hints
- Obstacles and grid boundaries split each row and column into maximal non-obstacle segments.
- Inside a horizontal segment [start, end], a cell at column c has left = c - start and right = end - c. A similar idea works for vertical segments.