Problem · Design
Mars Rover Robot Controller
Learn this problemProblem statement
You are controlling a rover on an infinite two-dimensional grid. The rover starts at coordinate (x, y) and faces one of four directions: N, E, S, or W.
You are given a command string containing the characters L, R, and M. Command L turns the rover left by 90 degrees, R turns it right by 90 degrees, and M moves it forward one cell in its current direction.
After executing all commands, return the rover's final position and direction in the format "x y D".
Function
getRoverPosition(start: int[], direction: String, commands: String) → StringExamples
Example 1
start = [0, 0]direction = "N"commands = "MRMLM"return = "1 2 N"The rover moves north to (0, 1), turns east, moves to (1, 1), turns north, and moves to (1, 2).
Example 2
start = [2, 3]direction = "E"commands = "MMRMM"return = "4 1 S"The rover moves east twice to (4, 3), turns south, then moves south twice to (4, 1).
Constraints
start.length == 2directionis one of"N","E","S", or"W".commandscontains onlyL,R, andM.- The grid is unbounded for this base version.