Simulate rover movement on a grid
Company: Shopify
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
You are building a rover navigation simulator.
## Part 1: Single rover on a 2D map
You are given:
- A rectangular 2D grid with coordinates `(x, y)`.
- A rover starting position `(x0, y0)` and a facing direction (one of `N, E, S, W`).
- A command string consisting of:
- `L`: turn left 90°
- `R`: turn right 90°
- `M`: move forward 1 step in the current facing direction
Rules:
- The rover cannot move outside the map boundaries. If a command would move it out of bounds, ignore that move (or alternatively, return an error—state your chosen behavior clearly and implement it consistently).
Task: Implement a function that returns the rover’s final position and direction after executing all commands.
## Part 2: Multiple rovers
Extend the design to support multiple rovers on the same 2D map.
- Each rover has its own initial state and command string.
- (Clarify your assumption) Either rovers execute sequentially (one after another) or simultaneously step-by-step.
- Decide and implement a collision policy (e.g., rovers cannot occupy the same cell; a move that would collide is rejected).
Task: Explain how you would change the code structure/API to support multiple rovers cleanly and safely.
## Part 3: 3D map
Now the map is 3D with coordinates `(x, y, z)`.
- Define a reasonable model for orientation in 3D (e.g., a heading direction plus pitch, or facing as one of 6 axes: `+X, -X, +Y, -Y, +Z, -Z`).
- Extend commands accordingly (you may introduce new commands if needed, but specify them).
Task: Describe and/or implement how your solution generalizes to 3D while keeping the design maintainable.
Quick Answer: This question evaluates spatial reasoning, state and command parsing, collision detection, boundary handling, and API design for extensible simulation across 2D and 3D coordinate systems.