Simulate robot path and detect boundedness
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates the ability to simulate iterative movement and reason about repeated state transformations, testing competencies in algorithm design, cycle detection, and geometric reasoning; it falls under the Coding & Algorithms category.
Constraints
- 1 <= len(s) <= 10^5 (s may also be empty in edge cases)
- s consists only of the characters 'G', 'L', and 'R'
- k (number of repetitions) can be up to 10^9 — do NOT simulate it
- The robot starts at (0, 0) facing North
Examples
Input: ('GGLLGG',)
Expected Output: [[0, 0], 'South', True]
Explanation: Move up to (0,2) facing North, two lefts flip to South, move back down to (0,0). Returns to origin, so bounded; final facing South.
Input: ('GG',)
Expected Output: [[0, 2], 'North', False]
Explanation: Two forward moves end at (0,2) still facing North. Displaced and still North => each repetition pushes further north forever => unbounded.
Input: ('GL',)
Expected Output: [[0, 1], 'West', True]
Explanation: Move to (0,1), then turn left to face West. Displaced but facing changed, so over 4 cycles the displacement cancels => bounded.
Input: ('',)
Expected Output: [[0, 0], 'North', True]
Explanation: Empty command: robot never moves, stays at origin facing North => bounded.
Input: ('GLGLGLGL',)
Expected Output: [[0, 0], 'North', True]
Explanation: A square loop: returns to origin facing North after one pass => bounded.
Input: ('R',)
Expected Output: [[0, 0], 'East', True]
Explanation: Just a right turn: no movement, facing East. At origin => bounded.
Input: ('GRGRGRG',)
Expected Output: [[0, 0], 'West', True]
Explanation: Traces a unit square clockwise back to (0,0), ending facing West => bounded.
Input: ('GGGGGGGG',)
Expected Output: [[0, 8], 'North', False]
Explanation: Eight forward moves to (0,8) still facing North => unbounded.
Input: ('LLGG',)
Expected Output: [[0, -2], 'South', True]
Explanation: Two lefts flip to South, then move to (0,-2). Displaced but facing changed => bounded.
Hints
- Represent the four headings as vectors in clockwise order: North=(0,1), East=(1,0), South=(0,-1), West=(-1,0). A right turn is +1 mod 4, a left turn is -1 mod 4 on the heading index.
- Simulate exactly one pass of s to get the final (x, y) and final heading. That single pass tells you everything.
- The path is bounded iff after one pass the robot is back at the origin, OR it is not facing North. If it ended displaced but still facing North, every repetition adds the same displacement vector forever, so it escapes to infinity.
- When the heading changed, the net effect of 4 passes is a full rotation that cancels the displacement, guaranteeing the robot stays within a bounded region — no need to test k beyond a single pass.