Simulate a Mars rover's turns and moves around obstacles and return its final position and distance
Quick Overview
Simulate a robot that turns left, turns right and steps forward on an unbounded 2D grid where some cells are blocked, then report its final coordinates and its Euclidean distance from the origin rounded to two decimals. It tests careful step-by-step simulation, direction bookkeeping and fast obstacle lookups.
Simulate a Mars rover's turns and moves around obstacles and return its final position and distance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Simulate a robot's movement on a 2D grid based on a sequence of commands and a set of obstacles. The robot starts at position `(0, 0)` facing North.
Given a string `commands` and a list of `obstacles` (each an `[x, y]` coordinate pair), execute the commands in order and return the robot's final position and Euclidean distance from the origin.
- `'L'` turns the robot left 90 degrees (for example, North to West).
- `'R'` turns the robot right 90 degrees (for example, North to East).
- `'M'` moves the robot forward one step in its current direction. If the next position is an obstacle, the robot stays in place and processes the next command.
Directions cycle as North (+y), East (+x), South (-y), West (-x).
Return a list of three values: the final x-coordinate, the final y-coordinate, and the Euclidean distance from the origin rounded to 2 decimal places.
### Function Signature
```python
def mars_rover(commands: str, obstacles: list[list[int]]) -> list[float]:
```
### Rules
- The grid is unbounded. Only the cells listed in `obstacles` block movement.
- A blocked `'M'` skips only that one step. The remaining commands still run.
- If an obstacle lies at `(0, 0)`, the robot still starts there. The obstacle only blocks later moves back into the origin.
- The result is `[x, y, distance]`, where `x` and `y` are integers and `distance` is $\sqrt{x^2 + y^2}$ rounded to 2 decimal places as a float. A whole-number distance such as 4 is returned as `4.0`.
### Constraints
- `0 <= len(commands) <= 10^4`
- `0 <= len(obstacles) <= 1000`
- `-10^4 <= obstacles[i][j] <= 10^4`
- `commands[i]` is one of `'L'`, `'R'`, `'M'`
### Examples
```text
Input: commands = "MMRMMLM", obstacles = [[2, 3]]
Output: [2, 2, 2.83]
```
The robot moves North to (0, 1) and (0, 2), turns East, moves to (1, 2) and (2, 2), then turns North, and its last move into (2, 3) is blocked. It ends at (2, 2), and $\sqrt{8} \approx 2.828$ rounds to 2.83.
```text
Input: commands = "MMMM", obstacles = []
Output: [0, 4, 4.0]
```
The robot moves North four times with nothing in the way and ends at (0, 4), so the distance is exactly 4.
```text
Input: commands = "LLMRMMM", obstacles = [[-3, -1]]
Output: [-2, -1, 2.24]
```
Two left turns point the robot South, so it moves to (0, -1). A right turn from South faces West, the robot moves to (-1, -1) and (-2, -1), and the last move into (-3, -1) is blocked; $\sqrt{5} \approx 2.236$ rounds to 2.24.
Overview: Simulate a robot that turns left, turns right and steps forward on an unbounded 2D grid where some cells are blocked, then report its final coordinates and its Euclidean distance from the origin rounded to two decimals. It tests careful step-by-step simulation, direction bookkeeping and fast obstacle lookups.
Simulate a Mars rover's turns and moves around obstacles and return its final position and distance
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0
Simulate a robot's movement on a 2D grid based on a sequence of commands and a set of obstacles. The robot starts at position (0, 0) facing North.
Given a string commands and a list of obstacles (each an [x, y] coordinate pair), execute the commands in order and return the robot's final position and Euclidean distance from the origin.
'L'
turns the robot left 90 degrees (for example, North to West).
'R'
turns the robot right 90 degrees (for example, North to East).
'M'
moves the robot forward one step in its current direction. If the next position is an obstacle, the robot stays in place and processes the next command.
Directions cycle as North (+y), East (+x), South (-y), West (-x).
Return a list of three values: the final x-coordinate, the final y-coordinate, and the Euclidean distance from the origin rounded to 2 decimal places.
The grid is unbounded. Only the cells listed in
obstacles
block movement.
A blocked
'M'
skips only that one step. The remaining commands still run.
If an obstacle lies at
(0, 0)
, the robot still starts there. The obstacle only blocks later moves back into the origin.
The result is
[x, y, distance]
, where
x
and
y
are integers and
distance
is
x2+y2
rounded to 2 decimal places as a float. A whole-number distance such as 4 is returned as
4.0
.
The robot moves North to (0, 1) and (0, 2), turns East, moves to (1, 2) and (2, 2), then turns North, and its last move into (2, 3) is blocked. It ends at (2, 2), and 8≈2.828 rounds to 2.83.
Two left turns point the robot South, so it moves to (0, -1). A right turn from South faces West, the robot moves to (-1, -1) and (-2, -1), and the last move into (-3, -1) is blocked; 5≈2.236 rounds to 2.24.