On an infinite plane, a robot initially stands at (0, 0)
and faces north. Note that:
The robot can receive one of three instructions:
"G"
: go straight 1 unit."L"
: turn 90 degrees to the left (i.e., anti-clockwise direction)."R"
: turn 90 degrees to the right (i.e., clockwise direction).The robot performs the instructions
given in order, and repeats them forever.
Return true
if and only if there exists a circle in the plane such that the robot never leaves the circle.
Example 1:
Input: instructions = "GGLLGG"
Output: true
Example 2:
Input: instructions = "GG"
Output: false
Example 3:
Input: instructions = "GL"
Output: true
Could you provide an algorithm and code to solve this problem? Also, discuss the time and space complexity of your solution, as well as potential edge cases.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine physically walking a robot around a grid following instructions. A brute force approach means we'd literally repeat the given instructions many, many times and see where the robot ends up.
Here's how the algorithm would work step-by-step:
def is_robot_bounded(instructions): initial_x_coordinate = 0
initial_y_coordinate = 0
current_direction = 'North'
number_of_repetitions = 4
for _ in range(number_of_repetitions):
for instruction in instructions:
if instruction == 'G':
if current_direction == 'North':
initial_y_coordinate += 1
elif current_direction == 'South':
initial_y_coordinate -= 1
elif current_direction == 'East':
initial_x_coordinate += 1
else:
initial_x_coordinate -= 1
elif instruction == 'L':
# Changing direction counter-clockwise
if current_direction == 'North':
current_direction = 'West'
elif current_direction == 'West':
current_direction = 'South'
elif current_direction == 'South':
current_direction = 'East'
else:
current_direction = 'North'
else:
# Changing direction clockwise
if current_direction == 'North':
current_direction = 'East'
elif current_direction == 'East':
current_direction = 'South'
elif current_direction == 'South':
current_direction = 'West'
else:
current_direction = 'North'
# Robot returns to origin after repetitions
if initial_x_coordinate == 0 and initial_y_coordinate == 0:
return True
# If robot direction changed it is bounded
if current_direction != 'North':
return True
return False
The key idea is that if the robot returns to its starting point OR faces the same direction it started in, it's bounded in a circle. We simulate the robot's moves and only need to track its position and direction after one full set of instructions.
Here's how the algorithm would work step-by-step:
def is_robot_bounded(instructions: str) -> bool:
current_direction = 0
current_x_position = 0
current_y_position = 0
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for instruction in instructions:
if instruction == 'G':
current_x_position += directions[current_direction][0]
current_y_position += directions[current_direction][1]
elif instruction == 'L':
# Turning left means decrementing direction index.
current_direction = (current_direction - 1) % 4
elif instruction == 'R':
# Turning right means incrementing direction index.
current_direction = (current_direction + 1) % 4
# Check if the robot returned to the origin or faces a different direction
if (current_x_position == 0 and current_y_position == 0) or current_direction != 0:
return True
else:
return False
Case | How to Handle |
---|---|
Empty instruction string | The robot doesn't move, so the end location is (0, 0) and direction is North, thus bounded. |
String contains only 'G' instructions | The robot moves in one direction indefinitely and is not bounded. |
String contains only 'L' or 'R' instructions | The robot rotates without moving, resulting in a bounded circle. |
String with very long sequence of instructions. | The solution should handle long instruction strings efficiently, likely with modular arithmetic to track direction. |
Integer overflow in coordinate calculations (very long 'G' sequence). | Use long or appropriate data type to avoid overflow when accumulating coordinates. |
Instructions perfectly cancel each other out (e.g., 'GLGLGLGL') | The robot ends up at the origin, and therefore it is bounded. |
Single rotation instruction ('L' or 'R'). | The robot rotates and stays at the origin (0,0), which means it is bounded. |
String with non 'G', 'L', 'R' character | The solution should throw an exception or handle it gracefully, for example by ignoring the unexpected characters. |