2069. Walking Robot Simulation II LeetCode Solution
In this guide, you will get 2069. Walking Robot Simulation II LeetCode Solution with the best time and space complexity. The solution to Walking Robot Simulation II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Walking Robot Simulation II solution in C++
- Walking Robot Simulation II solution in Java
- Walking Robot Simulation II solution in Python
- Additional Resources

Problem Statement of Walking Robot Simulation II
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.
Attempts to move forward one cell in the direction it is facing.
If the cell the robot is moving to is out of bounds, the robot instead turns 90 degrees counterclockwise and retries the step.
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 <= 100
1 <= num <= 105
At most 104 calls in total will be made to step, getPos, and getDir.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(\texttt{width} + \texttt{height})
2069. Walking Robot Simulation II LeetCode Solution in C++
class Robot {
public:
Robot(int width, int height) {
pos.push_back({{0, 0}, "South"});
for (int i = 1; i < width; ++i)
pos.push_back({{i, 0}, "East"});
for (int j = 1; j < height; ++j)
pos.push_back({{width - 1, j}, "North"});
for (int i = width - 2; i >= 0; --i)
pos.push_back({{i, height - 1}, "West"});
for (int j = height - 2; j > 0; --j)
pos.push_back({{0, j}, "South"});
}
void step(int num) {
isOrigin = false;
i = (i + num) % pos.size();
}
vector<int> getPos() {
return pos[i].first;
}
string getDir() {
return isOrigin ? "East" : pos[i].second;
}
private:
bool isOrigin = true;
int i = 0;
vector<pair<vector<int>, string>> pos;
};
/* code provided by PROGIEZ */
2069. Walking Robot Simulation II LeetCode Solution in Java
class Robot {
public Robot(int width, int height) {
pos.add(new Pair<>(new int[] {0, 0}, "South"));
for (int i = 1; i < width; ++i)
pos.add(new Pair<>(new int[] {i, 0}, "East"));
for (int j = 1; j < height; ++j)
pos.add(new Pair<>(new int[] {width - 1, j}, "North"));
for (int i = width - 2; i >= 0; --i)
pos.add(new Pair<>(new int[] {i, height - 1}, "West"));
for (int j = height - 2; j > 0; --j)
pos.add(new Pair<>(new int[] {0, j}, "South"));
}
public void step(int num) {
isOrigin = false;
i = (i + num) % pos.size();
}
public int[] getPos() {
return pos.get(i).getKey();
}
public String getDir() {
return isOrigin ? "East" : pos.get(i).getValue();
}
private boolean isOrigin = true;
private int i = 0;
private List<Pair<int[], String>> pos = new ArrayList<>();
}
// code provided by PROGIEZ
2069. Walking Robot Simulation II LeetCode Solution in Python
class Robot:
def __init__(self, width: int, height: int):
self.isOrigin = True
self.i = 0
self.pos = ([((0, 0), 'South')] +
[((i, 0), 'East') for i in range(1, width)] +
[((width - 1, j), 'North') for j in range(1, height)] +
[((i, height - 1), 'West') for i in range(width - 2, -1, -1)] +
[((0, j), 'South') for j in range(height - 2, 0, -1)])
def step(self, num: int) -> None:
self.isOrigin = False
self.i = (self.i + num) % len(self.pos)
def getPos(self) -> list[int]:
return self.pos[self.i][0]
def getDir(self) -> str:
return 'East' if self.isOrigin else self.pos[self.i][1]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.