Mars Rover Robot Controller
Updated
Problem 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.
More Shopify interview questions
- Deterministic Secret Santa AssignmentPHONE SCREEN · Seen Oct 2026
- Background Jobs with Exponential BackoffPHONE SCREEN · Seen Jan 2026
- Word-Guessing Game SessionPHONE SCREEN
- Card Game: Best First-Player ScoreOA · Seen Nov 2026
- Macaron Order with Set DiscountsOA · Seen Nov 2026
- Polynomial CoefficientsOA · Seen Nov 2026
- Choose Items for a BackpackOA · Seen May 2026
- Filter Words Containing a LetterOA · Seen Aug 2025
- Paint HouseOA · Seen Aug 2025
- Longest Increasing SubsequenceOA · Seen May 2025
Similar questions at other companies
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Recipe Rating ServiceNubank · PHONE SCREEN
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheAthenahealth · PHONE SCREEN
- Paged KV Cache AccessNVIDIA · PHONE SCREENNVIDIA interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Mutable Points in a Hash Set and a ListMillennium · PHONE SCREENMillennium interview questions