Robot Path That Repeats Forever
Updated
Problem statement
On an endless flat grid, a robot begins at the point (0, 0) facing north (toward larger y). The string instructions tells it what to do, one letter at a time:
'G': go one unit forward in the direction it faces.'L': a quarter turn counterclockwise, staying on the same point.'R': a quarter turn clockwise, staying on the same point.
After the last letter, the robot starts again from the first letter, and it keeps doing this forever.
Return true if some circle around the start holds the robot at every moment. Return false if no circle is big enough, because the robot wanders farther and farther away.
Function
isRobotBounded(instructions: String) → booleanExamples
Example 1
instructions = "GGRGGRGGRGGR"return = trueEach GGR walks two steps and turns right, so one round traces a 2 by 2 square: (0, 2), (2, 2), (2, 0) and back to (0, 0), facing north again. Every round repeats the same square, whose farthest corner (2, 2) is about 2.83 units from the start, so a circle of radius 3 around the start always contains the robot.
Example 2
instructions = "GLGR"return = falseAfter one round the robot is at (-1, 1) and faces north again. Each further round adds the same move, so it reaches (-2, 2), (-3, 3) and so on, getting farther away forever. No circle can hold it.
Example 3
instructions = "GGGRRG"return = trueOne round walks up to (0, 3), turns all the way around, and steps back to (0, 2), now facing south. The second round walks down to (0, -1), turns around again, and steps up to (0, 0), facing north. Every two rounds bring it home, and it is never more than 3 units from the start.
Constraints
1 <= instructions.length <= 2500instructionscontains only the letters'G','L'and'R'.
More OpenAI Online Assessment questions
- Count Routes Through Four Shop TypesOA · Seen Oct 2026
- Digit-Sum Sequence Join PointOA · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
- Password Clues and the Next CommandOA · Seen Oct 2026
- Longest Increasing Price SubsequenceOA · Seen Oct 2026
- Macaron Order PriceOA · Seen Oct 2026
- Shortest Atom Action PathOA · Seen Oct 2026
- Simplify a Quadratic ExpressionOA · Seen Oct 2026
- Minimum-Cost Color SequenceOA · PHONE SCREEN · Seen Oct 2026
- Largest Microorganism After ConsumptionOA · Seen Oct 2026
Similar questions at other companies
- Find Last CharacterGoogle · OAGoogle interview questions
- Count Prioritized Binary-Run OperationsZipRecruiter · OAZipRecruiter interview questions
- Flip Rows and Columns, Count ZerosNutanix · OANutanix interview questions
- Get Max DeletionsJP Morgan Chase · OAJP Morgan Chase interview questions
- FizzBuzzIndeed · OAIndeed interview questions
- FizzBuzzCapital One · OACapital One interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
- Add Decimal Strings ExactlyMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Format a Centered Pagination BarMixpanel · PHONE SCREENMixpanel interview questions