Walking Robot Simulation II - Problem

A robot is placed on a rectangular grid of width × height cells, with the bottom-left corner at position (0, 0) and the top-right corner at (width - 1, height - 1). The robot initially starts at (0, 0) facing East.

The robot can be instructed to move a specific number of steps. For each step:

  • It attempts to move forward one cell in its current direction
  • If the next cell is out of bounds, the robot turns 90 degrees counterclockwise and retries the step

Implement the Robot class with the following methods:

  • Robot(int width, int height): Initialize the grid and robot position
  • void step(int num): Move the robot forward num steps
  • int[] getPos(): Return current position as [x, y]
  • String getDir(): Return current direction ("North", "East", "South", "West")

Input & Output

Example 1 — Basic Robot Operations
$ Input: Robot(2, 3), step(2), getPos(), getDir(), step(2), getPos(), getDir(), step(2), getPos(), getDir()
Output: [null, null, [1,1], "North", null, [0,2], "West", null, [0,1], "South"]
💡 Note: Initialize 2×3 grid. Step 2: (0,0)→(1,0)→hit boundary→turn North→(1,1). Step 2: (1,1)→(1,2)→hit boundary→turn West→(0,2). Step 2: (0,2)→(0,1)→(0,0) would hit boundary→turn South→(0,1).
Example 2 — Single Cell Grid
$ Input: Robot(1, 1), step(2), getPos(), getDir()
Output: [null, null, [0,0], "East"]
💡 Note: In a 1×1 grid, robot cannot move and stays at (0,0) facing East regardless of steps.
Example 3 — Large Step Count
$ Input: Robot(2, 2), step(100), getPos(), getDir()
Output: [null, null, [0,0], "East"]
💡 Note: 2×2 grid has perimeter cycle of length 4. After 100 steps (100%4=0), robot returns to start position.

Constraints

  • 2 ≤ width, height ≤ 100
  • 1 ≤ num ≤ 105
  • At most 104 calls to step, getPos, and getDir

Visualization

Tap to expand
Walking Robot Simulation II INPUT 0 1 2 0 1 2 R Robot(width=2, height=3) Operations: 1. step(2) 2. getPos(), getDir() 3. step(2) 4. getPos(), getDir() 5. step(2) 6. getPos(), getDir() ALGORITHM STEPS 1 Calculate Perimeter P = 2*(w-1) + 2*(h-1) P = 2*1 + 2*2 = 6 2 Apply Modulo steps = steps % perimeter Handles cycles efficiently 3 Simulate Movement Move along boundary Turn at edges 4 Track Direction E --> N --> W --> S Counter-clockwise Path cycle FINAL RESULT 0 1 2 3 4 5 6 7 Output Sequence: After step(2): pos=[1,1], dir="North" After step(2): pos=[0,2], dir="West" After step(2): pos=[0,1], dir="South" Final Array: [null,null,[1,1], "North",...] Key Insight: Cycle Detection with Modular Arithmetic The robot travels along the grid's perimeter in a cycle. Using modulo operation (steps % perimeter) eliminates redundant full cycles and only simulates the remaining steps. This reduces time complexity from O(steps) to O(perimeter), making it efficient even for very large step counts. TutorialsPoint - Walking Robot Simulation II | Cycle Detection with Modular Arithmetic
Asked in
Google 12 Amazon 8 Microsoft 6 Apple 4
12.0K Views
Medium Frequency
~25 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen