Uber interview question
Find the robots whose distances to the nearest blocker match a query
- Company:
- Uber
- Type:
- Coding
- Difficulty:
- Medium
- Reported:
- October 19, 2026
Problem
A rectangular grid contains robots "O", empty cells "E" and blockers "X". Rows are numbered from the top and columns from the left, both starting at 0.
For a robot, its distance to the nearest blocker in a direction is the number of steps it would take moving in that direction to reach a blocker. The grid boundary also counts as a blocker: the cells just outside the grid behave like blockers, so a robot in column c with no blocker to its left has a left distance of c + 1. Robots and empty cells do not block.
Given grid and query = [left, top, bottom, right], return the positions [row, col] of every robot whose distances to the nearest blocker to the left, upward, downward and to the right equal the four query values. Return the positions in any order, or an empty list if no robot matches.
Example 1
Input:
grid = [["O", "E", "X", "O"],
["E", "O", "E", "E"],
["X", "E", "O", "E"]]
query = [2, 2, 2, 3]
Output: [[1, 1]]
The robot at (1, 1) is 2 steps from the left edge, 2 from the top edge, 2 from the bottom edge and 3 from the right edge. The other robots have distances [1, 1, 2, 2], [1, 1, 3, 1] and [2, 2, 1, 2].
Example 2
Input: grid = [["O", "X", "O"]], query = [1, 1, 1, 1]
Output: [[0, 0], [0, 2]]
Both robots touch the edge on three sides and the blocker in the middle on the fourth.
Constraints
- The grid has at least one row and one column, and every cell is
"O","E"or"X". - The query contains exactly four integers in the order left, top, bottom, right.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern