Walking Robot Simulation - Problem
Walking Robot Simulation is a fascinating problem that simulates a robot navigating an infinite grid plane!

Imagine a robot starting at the origin (0, 0) facing north. The robot receives a sequence of commands and must execute them while avoiding obstacles scattered across the grid.

Commands:
-2: Turn left 90 degrees
-1: Turn right 90 degrees
1-9: Move forward k units (one step at a time)

Key Rules:
• If the robot hits an obstacle, it stops at the current position and processes the next command
• The robot can start at (0, 0) even if there's an obstacle there, but cannot return once it leaves
North = +Y, East = +X, South = -Y, West = -X

Goal: Return the maximum squared Euclidean distance the robot reaches during its entire journey.

Input & Output

example_1.py — Basic Movement
$ Input: commands = [4,-1,4,-2,-1,5], obstacles = []
Output: 25
💡 Note: Robot moves North 4 steps to (0,4), turns right to face East, moves 4 steps to (4,4), turns left to face North, turns right to face East, moves 5 steps to (9,4). Maximum squared distance is 9² + 4² = 81 + 16 = 97. Wait, let me recalculate: (0,4) distance = 16, (4,4) distance = 32, (9,4) distance = 97. Actually, the robot stops at various points, max is 97.
example_2.py — With Obstacles
$ Input: commands = [4,-1,3], obstacles = [[2,0]]
Output: 25
💡 Note: Robot tries to move North 4 steps but there's an obstacle at (2,0). Robot moves to (0,1), (0,2), then hits obstacle at (2,0) - wait, that's wrong. Robot starts at (0,0) facing North, moves to (0,1), (0,2), (0,3), (0,4). Then turns right to face East. Then moves East 3 steps to (3,4). Max distance is 3² + 4² = 25.
example_3.py — Blocked Movement
$ Input: commands = [1,2,-1,2], obstacles = [[0,1]]
Output: 4
💡 Note: Robot starts at (0,0) facing North. Moves 1 step but blocked by obstacle at (0,1), stays at (0,0). Moves 2 steps but still blocked. Turns right to face East. Moves 2 steps to (2,0). Maximum squared distance is 2² + 0² = 4.

Visualization

Tap to expand
Robot Walking SimulationOrigin (0,0)RobotObstacleSample pathCommands-2: Turn left 90°-1: Turn right 90°1-9: Move forwardDirectionsNorth: +Y (up)East: +X (right)South: -Y, West: -XHash Set BenefitsO(1) obstacle lookupFast collision detectionOptimal performance
Understanding the Visualization
1
Initialize
Robot starts at origin (0,0) facing North, obstacles converted to hash set
2
Process Commands
Handle turning commands (-2 left, -1 right) and movement commands (1-9)
3
Step-by-Step Movement
For each unit of movement, check if next position has obstacle
4
Track Maximum
Update maximum squared distance whenever robot moves to new position
Key Takeaway
🎯 Key Insight: Hash set optimization transforms O(N×M×K) brute force into O(N×K+M) optimal solution by enabling instant obstacle lookups during step-by-step simulation.

Time & Space Complexity

Time Complexity
⏱️
O(N × K + M)

N commands × K steps per command + M obstacles for set creation

n
2n
Linear Growth
Space Complexity
O(M)

Hash set stores M obstacles

n
2n
Linear Space

Constraints

  • 1 ≤ commands.length ≤ 104
  • commands[i] is either -2, -1, or an integer in the range [1, 9]
  • 0 ≤ obstacles.length ≤ 104
  • -3 × 104 ≤ xi, yi ≤ 3 × 104
  • The answer is guaranteed to be less than 231
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
892 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