PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

Determine if robot path is bounded evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Oracle
  • Coding & Algorithms
  • Software Engineer

Determine if robot path is bounded

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

A robot starts at (0, 0) facing North on an infinite grid. It executes a string s consisting of 'G' (move forward 1 unit), 'L' (turn left 90°), and 'R' (turn right 90°) once, then repeats s forever. Return true if the robot stays within a bounded circle; otherwise return false. Justify your reasoning, discuss edge cases, and provide code.

Quick Answer: Determine if robot path is bounded evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

A robot starts at position (0, 0) on an infinite grid, initially facing North. It is given a string `instructions` consisting of three kinds of characters: - `'G'`: move forward 1 unit in the current direction. - `'L'`: turn 90 degrees to the left (counter-clockwise) without moving. - `'R'`: turn 90 degrees to the right (clockwise) without moving. The robot executes the entire string once, then repeats the same string forever. Return `true` if and only if there exists a circle of finite radius such that the robot never leaves it (i.e. its path is bounded); otherwise return `false`. The crucial observation: simulate exactly ONE pass over `instructions`. After one pass, the robot is at some offset `(x, y)` and facing some direction. The path is bounded if and only if (a) it ends back at the origin `(0, 0)`, OR (b) it does NOT end up facing North (its original direction). If it ends facing North but displaced from the origin, every cycle adds the same nonzero displacement and the robot drifts to infinity; if it ends facing any other direction, the cumulative rotation guarantees the net displacement cancels out within at most 4 cycles, keeping it within a bounded region.

Constraints

  • 1 <= instructions.length <= 100 (the empty string is also handled and is trivially bounded)
  • instructions consists only of the characters 'G', 'L', and 'R'
  • Coordinates fit comfortably in standard integer types within the constraint bounds

Examples

Input: ("GGLLGG",)

Expected Output: True

Explanation: Pass: G->(0,1), G->(0,2), L (now West), L (now South), G->(0,1), G->(0,0). Ends at origin, so bounded -> True.

Input: ("GG",)

Expected Output: False

Explanation: No turns: robot moves straight North forever, displacement (0,2) per cycle while still facing North. Unbounded -> False.

Input: ("GL",)

Expected Output: True

Explanation: G->(0,1) then L turns to West. Not facing North after one pass, so the four cycles trace a square and the path is bounded -> True.

Input: ("",)

Expected Output: True

Explanation: Empty instructions: robot never moves, stays at origin facing North. Trivially bounded -> True.

Input: ("G",)

Expected Output: False

Explanation: Single forward step, ends at (0,1) still facing North. Each cycle drifts further North. Unbounded -> False.

Input: ("RGRGRGRG",)

Expected Output: True

Explanation: Each R then G traces the four sides of a unit square, returning to the origin after one pass. Bounded -> True.

Input: ("LLLL",)

Expected Output: True

Explanation: Four left turns return to facing North with no movement; robot stays at the origin. Bounded -> True.

Input: ("GRGRGRG",)

Expected Output: True

Explanation: G->(0,1), R, G->(1,1), R, G->(1,0), R, G->(0,0). Ends back at the origin, so bounded -> True.

Hints

  1. You don't need to simulate forever — the behavior of all infinite repetitions is fully determined by ONE pass over the string. Track the final offset and final facing direction.
  2. Represent the four headings as direction vectors in clockwise order: North (0,1), East (1,0), South (0,-1), West (-1,0). A right turn is index +1 mod 4; a left turn is index +3 mod 4 (equivalently -1 mod 4).
  3. After one pass, the path is bounded iff the robot is back at the origin OR it is no longer facing North. If it faces North but is displaced, each cycle adds the same nonzero vector and it escapes to infinity. If it faces any other direction, the rotation makes the net 4-cycle displacement zero, so it stays bounded.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve Five Coding Problems - Oracle (medium)
  • Compute letter frequencies from encoded string - Oracle (medium)
  • Count closed islands in a grid - Oracle (easy)
  • Implement in-memory data structures and booking API - Oracle (hard)
  • Return a valid course completion order - Oracle (medium)