Skip to content

Practice Walking Robot Simulation II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2069
LeetCode Problem

Walking Robot Simulation II

Problem Statement

Robot(width,height) starts at (0,0), facing East. step(num) performs num successful single-cell moves. Before each move that would leave the grid, turn 90 degrees counterclockwise and retry. getPos() returns [x,y]; getDir() returns East, North, West, or South. Reaching a corner alone does not turn the robot. Practice approach: Track perimeter position modulo 2*(width+height-2), with a separate flag for whether any movement occurred. The initial origin faces East, but after a complete circuit it faces South. Convert perimeter position to coordinates and the direction of the last successful move.

Source: Walking Robot Simulation II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["Robot","getDir","step","getPos","getDir","step","getPos","getDir"]
[[3,2],[],[2],[],[],[4],[],[]]
Output
[null,"East",null,[2,0],"East",null,[0,0],"South"]
Explanation

At (2,0), the robot still faces East. Four further steps complete the perimeter and reach the origin facing South.

Constraints

2 <= width, height <= 100
1 <= num <= 10^5
At most 10^4 total calls.

Related LeetCode Problems

Practice more problems similar to Walking Robot Simulation II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.