Watch 3 video solutions for Walking Robot Simulation II, a medium level problem involving Design, Simulation. This walkthrough by Programming Live with Larry has 1,010 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A width x height grid is on an XY-plane with the bottom-left cell at (0, 0) and the top-right cell at (width - 1, height - 1). The grid is aligned with the four cardinal directions ("North", "East", "South", and "West"). A robot is initially at cell (0, 0) facing direction "East".
The robot can be instructed to move for a specific number of steps. For each step, it does the following.
After the robot finishes moving the number of steps required, it stops and awaits the next instruction.
Implement the Robot class:
Robot(int width, int height) Initializes the width x height grid with the robot at (0, 0) facing "East".void step(int num) Instructs the robot to move forward num steps.int[] getPos() Returns the current cell the robot is at, as an array of length 2, [x, y].String getDir() Returns the current direction of the robot, "North", "East", "South", or "West".
Example 1:
Input
["Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir"]
[[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
Output
[null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]
Explanation
Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East.
robot.step(2); // It moves two steps East to (2, 0), and faces East.
robot.step(2); // It moves two steps East to (4, 0), and faces East.
robot.getPos(); // return [4, 0]
robot.getDir(); // return "East"
robot.step(2); // It moves one step East to (5, 0), and faces East.
// Moving the next step East would be out of bounds, so it turns and faces North.
// Then, it moves one step North to (5, 1), and faces North.
robot.step(1); // It moves one step North to (5, 2), and faces North (not West).
robot.step(4); // Moving the next step North would be out of bounds, so it turns and faces West.
// Then, it moves four steps West to (1, 2), and faces West.
robot.getPos(); // return [1, 2]
robot.getDir(); // return "West"
Constraints:
2 <= width, height <= 1001 <= num <= 105104 calls in total will be made to step, getPos, and getDir.Problem Overview: Design a robot that walks along the boundary of a width × height grid. The robot starts at (0,0) facing East, moves step by step, turns counter‑clockwise at borders, and must report its current position and direction after any sequence of moves.
Approach 1: Direct Grid Simulation (O(k) time per move, O(1) space)
The straightforward solution simulates the robot one step at a time. Maintain the robot's (x, y) position and direction. For each step in move(num), attempt to move forward; if the next cell would leave the grid, rotate the direction counter‑clockwise and try again. This mirrors the exact rules of the problem and is easy to implement using a direction array and boundary checks. The downside is performance: a call like move(10^9) requires iterating all steps, making the method inefficient for large inputs. This approach mainly demonstrates the mechanics of simulation.
Approach 2: Circular Path Simplification (O(1) time per move, O(1) space)
The key observation: the robot never enters the interior of the grid. It only walks along the perimeter. The total perimeter cycle length is 2 × (width + height) − 4. Any movement larger than this simply repeats the same loop. Instead of simulating each step, reduce the movement using num % perimeter. Then advance along the four edges in order: bottom edge (east), right edge (north), top edge (west), and left edge (south). Update position and direction based on which segment the remaining steps fall into. Because the perimeter has only four segments, the update logic runs in constant time. This turns a potentially massive simulation into a small arithmetic calculation and fits naturally with a design style problem.
Recommended for interviews: The circular path simplification is the expected solution. It shows you recognized the perimeter cycle and eliminated unnecessary simulation. Implementing the direct simulation first can help verify the movement rules, but the optimized perimeter approach demonstrates stronger algorithmic thinking and system design awareness.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Grid Simulation | O(k) per move | O(1) | When implementing a simple simulation or validating movement logic step by step |
| Circular Path Simplification | O(1) per move | O(1) | Best approach when moves can be very large since the robot only cycles along the grid perimeter |