Implement a Mars rover that follows turn and move commands on a bounded grid
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
A robotic rover is to be landed on a rectangular plateau on Mars. The plateau is divided into a grid to simplify navigation. A rover's position is represented by `x` and `y` coordinates and a letter representing one of the four cardinal compass directions: `N`, `E`, `S`, `W`.
The plateau is bounded by its width and height (coordinates range from `0` to `width` on the x-axis and `0` to `height` on the y-axis). The rover cannot move outside these boundaries. If a move instruction would take the rover out of bounds, that move is **ignored** and the rover remains in place.
The rover receives a string of instructions where each character is `L` (turn left 90 degrees), `R` (turn right 90 degrees), or `M` (move forward one grid point in the current heading direction). Implement the `MarsRover` class to initialize the plateau and rover state, execute instruction strings, and report the rover's current position.
Implement the following methods:
- `MarsRover(width, height, x, y, direction)`: Create the plateau with the given width and height and place the rover at `(x, y)` facing `direction`.
- `execute(instructions: str)`: Execute a string of instructions. `L` turns the rover left 90 degrees, `R` turns right 90 degrees, `M` moves forward one grid point. Moves that would take the rover outside the plateau are ignored.
- `getPosition() -> str`: Return the rover's current position and heading as a string in the format `x y direction`.
Example 1:
```text
Input:
["MarsRover", "execute", "getPosition"]
[[5, 5, 1, 2, "N"], ["LMLMLMLMM"], []]
Output: [null, null, "1 3 N"]
Explanation:
rover = MarsRover(5, 5, 1, 2, 'N') # plateau 5x5, start at (1,2) facing N
rover.execute('LMLMLMLMM') # L->W, M->(0,2), L->S, M->(0,1), L->E, M->(1,1), L->N, M->(1,2), M->(1,3)
rover.getPosition() # -> '1 3 N'
```
Example 2:
```text
Input:
["MarsRover", "execute", "getPosition"]
[[5, 5, 3, 3, "E"], ["MMRMMRMRRM"], []]
Output: [null, null, "5 1 E"]
Explanation:
rover = MarsRover(5, 5, 3, 3, 'E') # plateau 5x5, start at (3,3) facing E
rover.execute('MMRMMRMRRM') # M->(4,3), M->(5,3), R->S, M->(5,2), M->(5,1), R->W, M->(4,1), R->N, R->E, M->(5,1)
rover.getPosition() # -> '5 1 E'
```
```hint Heading and moves
Choose a representation of the heading that turns `L` and `R` into simple arithmetic instead of a chain of cases, and work out where an `M` would land before you change any state.
```
### Constraints & Assumptions
- `0 <= width, height <= 100`
- `0 <= x <= width`, `0 <= y <= height`
- `direction` is one of `'N'`, `'E'`, `'S'`, `'W'`
- Instructions consist only of the characters `'L'`, `'R'`, `'M'`
- `1 <= instructions.length <= 1000`
- North is the direction of increasing `y` and east is the direction of increasing `x`, as the examples show.
- An ignored move leaves the rover's position and heading unchanged, and the remaining instructions still run.
### What a Strong Answer Covers
- A heading representation where `L` and `R` are constant-time updates that wrap correctly in both directions.
- Moving by a per-heading step in `x` and `y`, with the target cell checked against the inclusive bounds before the rover's state changes.
- Correct handling of ignored moves: the rover keeps its position and heading and goes on with the next instruction.
- A `getPosition` result in exactly the `x y direction` format, and the time and space cost of `execute`.
- Edge cases such as a 0x0 plateau, a rover that starts on an edge, and repeated `execute` calls that continue from the current state.
### Follow-up Questions
- How would you add obstacle cells that block a move, and how would the rover report that a move was blocked?
- Several rovers share the plateau and run their instruction strings one after another. How do you stop two rovers from occupying the same cell?
- How would you add a new command, such as `B` for one step backward, without growing a chain of conditionals inside `execute`?
- Which unit tests would you write for this class, and which of them would you expect to catch real bugs?
Overview: Design a class that tracks a rover's position and compass heading on a bounded rectangular grid and runs strings of left, right and move commands. It tests state modeling, boundary checks for moves that would leave the grid, and returning the final position in an exact string format.