Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Robot Bounded In Circle

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1041" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that: The north direction is the positive direction of the y-axis. The south direction is the negative direction of the y-axis. The east direction is the positive direction of the x-axis. The west direction is the negative direction of the x-axis. 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 Explanation: The robot is initially at (0, 0) facing the north direction. "G": move one step. Position: (0, 1). Direction: North. "G": move one step. Position: (0, 2). Direction: North. "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West. "L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South. "G": move one step. Position: (0, 1). Direction: South. "G": move one step. Position: (0, 0). Direction: South. Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0). Based on that, we return true. Example 2: Input: instructions = "GG" Output: false Explanation: The robot is initially at (0, 0) facing the north direction. "G": move one step. Position: (0, 1). Direction: North. "G": move one step. Position: (0, 2). Direction: North. Repeating the instructions, keeps advancing in the north direction and does not go into cycles. Based on that, we return false. Example 3: Input: instructions = "GL" Output: true Explanation: The robot is initially at (0, 0) facing the north direction. "G": move one step. Position: (0, 1). Direction: North. "L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West. "G": move one step. Position: (-1, 1). Direction: West. "L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South. "G": move one step. Position: (-1, 0). Direction: South. "L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East. "G": move one step. Position: (0, 0). Direction: East. "L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North. Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0). Based on that, we return true. Constraints: 1 <= instructions.length <= 100 instructions[i] is 'G', 'L' or, 'R'.

Explanation

Here's the breakdown of the solution:

  • Simulate the robot's movement: Execute the given instructions once and track the change in position (x, y) and the final direction the robot is facing.
  • Check for cycle or bounded path: The robot stays within a circle if either it returns to the origin (0, 0) after executing the instructions once, or it doesn't end up facing North. If it ends up facing North and also has moved away from the origin, it will go to infinity.

  • Time Complexity: O(n), where n is the length of the instructions string.

  • Space Complexity: O(1)

Code

    def isRobotBounded(instructions: str) -> bool:
    """
    Determines if a robot, following a set of instructions repeatedly, stays within a bounded circle.

    Args:
        instructions: A string representing the robot's instructions ('G', 'L', or 'R').

    Returns:
        True if the robot stays within a bounded circle, False otherwise.
    """

    x, y = 0, 0  # Initial position
    direction = 0  # 0: North, 1: East, 2: South, 3: West
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]  # North, East, South, West

    for instruction in instructions:
        if instruction == 'G':
            dx, dy = directions[direction]
            x += dx
            y += dy
        elif instruction == 'L':
            direction = (direction - 1) % 4
        else:  # instruction == 'R'
            direction = (direction + 1) % 4

    # If the robot returns to the origin or does not face North, it's bounded
    return (x == 0 and y == 0) or direction != 0

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Robot Bounded In Circle