Determine if robot path is bounded
Company: Oracle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: Determine if robot path is bounded evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 1 <= instructions.length <= 100 (the empty string is also handled and is trivially bounded)
- instructions consists only of the characters 'G', 'L', and 'R'
- Coordinates fit comfortably in standard integer types within the constraint bounds
Examples
Input: ("GGLLGG",)
Expected Output: True
Explanation: Pass: G->(0,1), G->(0,2), L (now West), L (now South), G->(0,1), G->(0,0). Ends at origin, so bounded -> True.
Input: ("GG",)
Expected Output: False
Explanation: No turns: robot moves straight North forever, displacement (0,2) per cycle while still facing North. Unbounded -> False.
Input: ("GL",)
Expected Output: True
Explanation: G->(0,1) then L turns to West. Not facing North after one pass, so the four cycles trace a square and the path is bounded -> True.
Input: ("",)
Expected Output: True
Explanation: Empty instructions: robot never moves, stays at origin facing North. Trivially bounded -> True.
Input: ("G",)
Expected Output: False
Explanation: Single forward step, ends at (0,1) still facing North. Each cycle drifts further North. Unbounded -> False.
Input: ("RGRGRGRG",)
Expected Output: True
Explanation: Each R then G traces the four sides of a unit square, returning to the origin after one pass. Bounded -> True.
Input: ("LLLL",)
Expected Output: True
Explanation: Four left turns return to facing North with no movement; robot stays at the origin. Bounded -> True.
Input: ("GRGRGRG",)
Expected Output: True
Explanation: G->(0,1), R, G->(1,1), R, G->(1,0), R, G->(0,0). Ends back at the origin, so bounded -> True.
Hints
- You don't need to simulate forever — the behavior of all infinite repetitions is fully determined by ONE pass over the string. Track the final offset and final facing direction.
- Represent the four headings as direction vectors in clockwise order: North (0,1), East (1,0), South (0,-1), West (-1,0). A right turn is index +1 mod 4; a left turn is index +3 mod 4 (equivalently -1 mod 4).
- After one pass, the path is bounded iff the robot is back at the origin OR it is no longer facing North. If it faces North but is displaced, each cycle adds the same nonzero vector and it escapes to infinity. If it faces any other direction, the rotation makes the net 4-cycle displacement zero, so it stays bounded.