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.
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.