Robot Bounded In Circle Solution
On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
- “G”: go straight 1 unit;
- “L”: turn 90 degrees to the left;
- “R”: turn 90 degrees to the right.
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
Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0). When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
Example 2:
Input: instructions = “GG”
Output: false
Explanation: The robot moves north indefinitely.
Example 3:
Input: instructions = “GL”
Output: true
Explanation: The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> …
Constraints:
- 1 <= instructions.length <= 100
- instructions[i] is ‘G’, ‘L’ or, ‘R’.

SOLUTION
Program: Robot Bounded In Circle Solution in C++
class Solution {
public:
int dx[4]{0, 0, -1, 1};
int dy[4]{1, -1, 0, 0};
int turnLeft(int d) {
if (d == 0) return 2;
if (d == 2) return 1;
if (d == 1) return 3;
return 0;
}
int turnRight(int d) {
if (d == 0) return 3;
if (d == 3) return 1;
if (d == 1) return 2;
return 0;
}
bool isRobotBounded(string instructions) {
int x, y, direct;
x = y = direct = 0;
instructions += instructions + instructions + instructions;
for(auto c : instructions)
if (c == 'L')
direct = turnLeft(direct);
else if (c == 'R')
direct = turnRight(direct);
else {
x += dx[direct];
y += dy[direct];
}
if (x == 0 && y == 0) return true;
return false;
}
};
Program: Robot Bounded In Circle Solution in Java
class Solution {
public boolean isRobotBounded(String instructions) {
// init position
int[] pos = new int[]{0, 0};
// directions north, east, south, west
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int face = 0;
char[] ins = instructions.toCharArray();
for(char c: ins) {
if(c == 'L') {
face = face == 0 ? 3 : face - 1;
}
else if(c == 'R') {
face = face == 3 ? 0 : face + 1;
}
else {
pos[0] = pos[0] + dirs[face][0];
pos[1] = pos[1] + dirs[face][1];
}
}
return (face != 0 || (pos[0] == 0 && pos[1] == 0));
}
}
Program: Robot Bounded In Circle Solution in Python
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
offset_dict_L = {}
offset_dict_L[(0,1)] = (-1,0)
offset_dict_L[(0,-1)] = (1,0)
offset_dict_L[(1,0)] = (0,1)
offset_dict_L[(-1,0)] = (0,-1)
offset_dict_R = {}
offset_dict_R[(0,1)] = (1,0)
offset_dict_R[(0,-1)] = (-1,0)
offset_dict_R[(1,0)] = (0,-1)
offset_dict_R[(-1,0)] = (0,1)
cx = 0
cy = 0
c_offset = (0,1)
for i in instructions:
if(i == 'G'):
cx += c_offset[0]
cy += c_offset[1]
elif(i == 'L'):
c_offset = offset_dict_L.get(c_offset)
elif(i == 'R'):
c_offset = offset_dict_R.get(c_offset)
if(cx == 0 and cy == 0):
return True
elif(c_offset == (0,1)):
return False
else:
return True
Amazon OA 2023 Questions with Solution
- Shopping Patterns Solution Amazon OA 2023
- Reorder Data in Log Files Solution Amazon OA 2023
- Top K Frequent Words Solution Amazon OA 2023
- Trees Height Solution Amazon OA SDE 2023
- Counting Binary Substrings Amazon OA 2023
- Grid Connections Amazon OA 2023
- Shipment Imbalance Amazon OA 2023
- Max Profit Amazon OA 2023
- Find Lowest Price Amazon OA 2023
- Decode String Frequency Amazon OA 2023
- Simple Cipher Amazon OA 2023
- Valid Discount Coupons Amazon OA 2023 Solution
- Count Maximum Teams Amazon OA 2023
- Minimum Coin Flips Amazon OA 2023
- Max Average Stock Price Amazon OA 2023 Solution
- Shopping Options Amazon OA 2023 Solution
- Fill The Truck Maximum Units on a Truck Amazon OA Solution
- Maximize Score After N Operations Number Game Solution Amazon OA 2023
- Slowest Key Amazon OA 2023 Solution
- Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
- Split String Into Unique Primes Amazon OA 2023 Solution
- Storage Optimization Amazon OA 2023 Solution
- Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
- Autoscale Policy Utilization Check Amazon OA 2023
- Optimal Utilization Solution Amazon OA 2023
- Merge Two Sorted Lists Solution Amazon OA 2023
- Two Sum Unique Pairs Solution Amazon OA 2023
- Amazon Music Pairs Amazon OA 2023 Solution
- Class Grouping Amazon OA 2023 Solution
- Find Max products Amazon OA 2023 Solution
- Get encrypted number Amazon OA 2023 Solution
- Find Total Imbalance Amazon OA 2023 Solution
- Find Total Power Amazon OA 2023 Solution