Implement a multi-rover Mars controller
ShopifySoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to implement stateful command parsing, entity management for multiple rovers, and collision-avoidance on an unbounded 2D grid, emphasizing deterministic simulation, input/output specification, and robust handling of invalid operations.
Problem
Build a small command-line “Mars Rover Controller” program.
Single rover (v1)
A rover starts at coordinate (0, 0) on an infinite 2D grid and initially faces North.
The controller must support these commands:
L: turn left 90° (N→W→S→E→N)R: turn right 90° (N→E→S→W→N)M: move forward 1 cell in the direction the rover is currently facing
After each command is processed, the program must report the rover’s current position and facing direction.
Multiple rovers (v2)
Extend the controller to manage multiple rovers and support:
CREATE <id>: create a new rover with the default starting state (0,0,N)DELETE <id>: delete a roverSELECT <id>: make the rover with<id>the active rover- Movement/turn commands (
L,R,M) apply to the currently selected rover
Define behavior for invalid operations, e.g. selecting or deleting a non-existent rover, or issuing L/R/M with no rover selected.
Collision avoidance (v3)
Add collision prevention:
- If the selected rover executes
Mand the target cell is already occupied by another rover, the move must fail and the rover must remain in its current position/direction. - The program must still report state after the command; you may choose an error/status message format.
Input / Output
You may design the exact CLI format (interactive REPL or reading lines from stdin), but it must be unambiguous and testable.
At minimum, specify:
- Accepted command syntax
- What is printed after each command (position + direction, and optionally status on failures)
Constraints / Notes
- Grid is unbounded; coordinates may be negative.
- Rover IDs are unique strings.
- Aim for clean, testable design; include unit tests.
Example (one possible interaction)
CREATE a
SELECT a
M
R
M
CREATE b
SELECT b
M
SELECT a
M # fails if b is at the target cell
(Your exact printed output format may differ, but it must include position + facing after each command.)
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.