The key insight I kept circling around was that you only need to simulate one cycle.
First, simulate one pass of the command string to compute the net displacement and final direction. Then, analyze the repeated behavior: if the robot returns to the origin or faces north after one pass, the path is bounded; otherwise, it's unbounded. This avoids simulating all repetitions and gives O(n) time and O(1) space.
Pro tip: Mention that the boundedness condition can be determined by checking if the robot's final direction is north or if it returns to the origin after one pass—this shows you understand the mathematical insight behind the problem.
Iterate through the command string once, updating the robot's position (x, y) and direction (dx, dy) based on each instruction.
After one pass, record the net displacement vector (x, y) and the final direction vector (dx, dy).
If the final direction is north (0,1), the path is bounded. If the net displacement is (0,0), the path is bounded. Otherwise, the path is unbounded.
Time complexity is O(n) for one pass, and space complexity is O(1). No need to simulate all repetitions.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.