Simulate a rover fleet
Company: Shopify
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates skills in simulation design, state management, grid-based movement logic, and handling constraints such as obstacles, boundaries, and sequential occupancy.
Constraints
- 1 <= width, height <= 10^5
- 0 <= number of obstacles <= 2 * 10^5
- 0 <= number of rovers <= 10^5
- The total length of all command strings is at most 2 * 10^5
- Obstacle cells are unique and lie inside the grid
- Each rover starts inside the grid and not on an obstacle cell
- Commands contain only 'M', 'L', and 'R'
Examples
Input: (4, 4, [(1, 1)], [(0, 0, 'N', 'MMR'), (0, 1, 'N', 'M'), (2, 2, 'W', 'LM')])
Expected Output: [(0, 2, 'E', False), (0, 1, 'N', True), (2, 1, 'S', False)]
Explanation: The first rover finishes at (0, 2) facing east, so that cell becomes occupied. The second rover tries to move into (0, 2) and is blocked. The third rover turns left from west to south, then moves to (2, 1).
Input: (3, 3, [], [(0, 0, 'S', 'MML'), (2, 2, 'E', 'M')])
Expected Output: [(0, 0, 'S', True), (2, 2, 'E', True)]
Explanation: Both rovers attempt to move outside the grid on their first move, so each one stops immediately and remains in place.
Input: (5, 5, [(2, 3), (3, 1)], [(1, 1, 'N', 'MRMLM'), (2, 1, 'N', 'M')])
Expected Output: [(2, 2, 'N', True), (2, 1, 'N', True)]
Explanation: The first rover reaches (2, 2) and then tries to move into obstacle (2, 3), so it is blocked there. That final cell becomes occupied, and the second rover is then blocked when trying to move into (2, 2).
Input: (2, 2, [(1, 1)], [])
Expected Output: []
Explanation: There are no rovers to simulate, so the result is an empty list.
Input: (1, 1, [], [(0, 0, 'N', 'LRM')])
Expected Output: [(0, 0, 'N', True)]
Explanation: On a 1x1 grid, the rover can turn left and right in place, but any move goes out of bounds, so it ends blocked at its starting cell.
Hints
- Use sets for obstacle cells and occupied final cells so you can check whether a move is valid in O(1) average time.
- Represent directions in a fixed circular order like ['N', 'E', 'S', 'W'] so turning left or right is just index arithmetic.