FastPrepEncircular

Encircular

Goldman Sachs logoGoldman SachsMediumNEW GRADINTERNOA
Learn

Problem statement

A robot starts at (0, 0) on an infinite plane and initially faces north. A command string contains only:

  • G: move forward one unit in the current direction
  • L: turn left by 90 degrees without moving
  • R: turn right by 90 degrees without moving

The robot repeats the entire command string forever. A command is circular if the robot always remains inside some finite circle.

For every string in commands, return "YES" if its repeated path is circular, or "NO" otherwise, preserving input order.

Function

doesCircleExist(commands: String[]) → String[]

Examples

Example 1

commands = ["G", "L", "RGRG"]return = ["NO", "YES", "YES"]
  1. G moves forward forever, so its result is NO.
  2. L only rotates at the origin, so its result is YES.
  3. Repeating RGRG follows a bounded path, so its result is YES.

Constraints

  • 1 <= commands.length <= 10
  • 1 <= commands[i].length <= 2500
  • Each command contains only G, L, and R.

More Goldman Sachs problems

See Goldman Sachs hiring insights
public String[] doesCircleExist(String[] commands) {
  // write your code here
}
commands["G", "L", "RGRG"]
expected["NO", "YES", "YES"]
Checking account…