Robot Bounded In Circle

MediumMathsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A robot starts at (0, 0) on an infinite grid facing north. It follows a string instructions of these commands: - 'G': move one unit forward in the current direction. - 'L': turn 90 degrees left (counter-clockwise) in place. - 'R': turn 90 degrees right (clockwise) in place. The robot runs instructions in order and then repeats the whole string forever. Return true if there is some circle on the plane that the robot never leaves, and false if it drifts arbitrarily far away.

Examples

Input: instructions = "GGLLGG"
Output: true
The robot walks to (0,2), turns around and walks back to (0,0) facing south; it keeps retracing the same segment.
Input: instructions = "GG"
Output: false
Each repetition moves the robot 2 units further north.
Input: instructions = "GL"
Output: true
After four repetitions the robot has walked a unit square and is back at the origin facing north.

Constraints

  • 1 <= instructions.length <= 100
  • instructions[i] is 'G', 'L' or 'R'

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems