Coding questions · Uber
Simulate robot on grid with obstacles
Simulate robot on grid with obstacles evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
A robot starts at (0, 0) on an unbounded integer grid. You are given a sequence of commands consisting of characters U, D, L, and R, and a set of blocked cells (obstacles). For each command, the robot attempts to move one cell in the indicated direction; if the destination cell is blocked, it does not move for that command and proceeds to the next. Return the maximum squared Euclidean distance from the origin reached at any time during the simulation. Design an algorithm that handles up to 1e5 commands and 1e5 obstacles efficiently; describe data structures (e.g., hash set for obstacles), complexity, and edge cases.
What a Strong Answer Covers
- A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
- A correctness argument that explains why the algorithm covers all required cases.
- Time and space complexity, plus at least one alternative approach when relevant.
- Focused tests for normal cases, edge cases, and failure modes.
Follow-up Questions
- How would the approach change if the input were streaming or too large for memory?
- What invariants would you assert in production code?
- Which tests would catch off-by-one, duplicate, or tie-breaking bugs?
Simulate Robot on Grid with Obstacles
A robot starts at (0, 0) on an unbounded integer grid. You are given a string commands of characters from {U, D, L, R} and a list obstacles of blocked cells, each given as [x, y].
For each command, the robot attempts to move one cell in the indicated direction:
U-> (x, y+1)D-> (x, y-1)L-> (x-1, y)R-> (x+1, y)
If the destination cell is blocked (an obstacle), the robot does NOT move for that command and proceeds to the next command. Otherwise it moves into the destination cell.
Return the maximum squared Euclidean distance from the origin (x*x + y*y) reached at ANY point during the simulation, including intermediate positions. If the robot never leaves the origin, return 0.
The algorithm must handle up to 1e5 commands and 1e5 obstacles efficiently. Store obstacles in a hash set for O(1) lookup; the simulation is then a single O(number_of_commands) pass.
Constraints
- 0 <= len(commands) <= 1e5
- commands contains only the characters 'U', 'D', 'L', 'R'
- 0 <= len(obstacles) <= 1e5
- Each obstacle is a pair [x, y] of integers; the origin (0, 0) is never an obstacle
- Coordinates fit comfortably in a 64-bit integer; the squared distance can exceed 2^31, so use a 64-bit return type in Java/C++ if commands push far in one direction
Examples
Example 1
Input: ("RRUU", [])
Output: 8
Explanation: No obstacles. Path: (1,0)->(2,0)->(2,1)->(2,2). Final (2,2) gives 2^2+2^2 = 8, the maximum.
Example 2
Input: ("RRUU", [[2, 0]])
Output: 5
Explanation: Cell (2,0) is blocked. First R reaches (1,0); second R targets (2,0) -> blocked, stay at (1,0). Then U->(1,1), U->(1,2). Max is at (1,2): 1+4 = 5.
Hints
- Store obstacles in a hash set keyed by the (x, y) pair so you can test 'is this cell blocked?' in O(1).
- Walk the commands one at a time, keeping the current (x, y). Compute the destination; if it is in the blocked set, skip the move entirely and keep the old position.
- Track the maximum of xx + yy after every successful move (not just at the end) — the farthest point can be an intermediate position the robot later moves away from.
- The origin contributes distance 0, so initialize the best answer to 0; that also handles the empty-command and never-moves cases.
Solution
Solution Alignment
The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.
Problem Restatement
A robot starts at (0, 0) on an unbounded integer grid. You are given a sequence of commands consisting of characters U, D, L, and R, and a set of blocked cells (obstacles). For each command, the robot attempts to move one cell in the indicated direction; if the destination cell is blocked, it does not move for that command and proceeds to the next. Return the maximum squared Euclidean distance from the origin reached at any time during the simulation. Design an algorithm that handles up to 1e5 commands and 1e5 obstacles efficiently; describe data structures (e.g., hash set for obstacles), complexity, and edge cases.
Recommended Approach
Represent each cell as a state. Use BFS for minimum-distance propagation, DFS with memoization for longest monotonic paths, and careful boundary checks for simulation. Store obstacles or visited cells in sets when the grid is sparse.
Correctness
The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.
Complexity
A full grid traversal is O(mn) time and O(mn) space in the worst case. Sparse simulation is O(number_of_commands) plus obstacle storage.
Edge Cases and Tests
Empty grid, one row/column, blocked start or target, boundaries, repeated visits, and tie-breaking among equal-distance cells.