Walking Robot Simulation

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
A robot on an infinite XY-plane starts at point (0, 0) facing north. The robot receives an array of integers commands, which represents a sequence of moves that it needs to execute. There are only three possible types of instructions the robot can receive:
-2: Turn left90degrees.-1: Turn right90degrees.1 <= k <= 9: Move forwardkunits, one unit at a time.
Some of the grid squares are obstacles. The i<sup>th</sup> obstacle is at grid point obstacles[i] = (x<sub>i</sub>, y<sub>i</sub>). If the robot runs into an obstacle, it will stay in its current location (on the block adjacent to the obstacle) and move onto the next command.
Return the maximum squared Euclidean distance that the robot reaches at any point in its path (i.e. if the distance is 5, return 25).
Note:
There can be an obstacle at
(0, 0). If this happens, the robot will ignore the obstacle until it has moved off the origin. However, it will be unable to return to(0, 0)due to the obstacle.North means +Y direction.
East means +X direction.
South means -Y direction.
West means -X direction.
LeetCode Problem - 874
class Solution {
public int robotSim(int[] commands, int[][] obstacles) {
// Directions array representing North, East, South, and West
// North: {0,1}, East: {1,0}, South: {0,-1}, West: {-1,0}
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
// Starting position of the robot at (0, 0)
int[] curPos = {0, 0};
// Variable to store the maximum squared distance
int result = 0;
// Variable to keep track of the current direction (0 -> North, 1 -> East, 2 -> South, 3 -> West)
int curDir = 0;
// Map to store obstacles: key is the x-coordinate, value is a set of y-coordinates
// This helps in quick lookup for checking obstacles at specific coordinates
Map<Integer, HashSet<Integer>> obstacleMap = new HashMap<>();
for (int[] obstacle : obstacles) {
// If the x-coordinate is not yet in the map, create a new set for it
if (!obstacleMap.containsKey(obstacle[0])) {
obstacleMap.put(obstacle[0], new HashSet<>());
}
// Add the y-coordinate to the set of obstacles for that x-coordinate
obstacleMap.get(obstacle[0]).add(obstacle[1]);
}
// Loop through each command in the commands array
for (int command : commands) {
// If the command is -1, turn right (clockwise)
if (command == -1) {
curDir = (curDir + 1) % 4; // Update the current direction
continue;
}
// If the command is -2, turn left (counterclockwise)
if (command == -2) {
curDir = (curDir - 1); // Update the current direction
if (curDir == -1) curDir = 3; // Wrap the direction index to stay in bounds
continue;
}
// Get the current direction from the directions array
int[] direction = directions[curDir];
// Move the robot step by step in the current direction
for (int step = 0; step < command; step++) {
// Calculate the next position the robot will move to
int nextX = curPos[0] + direction[0];
int nextY = curPos[1] + direction[1];
// If the next position is an obstacle, stop moving in this direction
if (obstacleMap.containsKey(nextX) && obstacleMap.get(nextX).contains(nextY)) {
break;
}
// Update the robot's current position to the new position
curPos[0] = nextX;
curPos[1] = nextY;
}
// Update the result with the maximum Euclidean distance squared from the origin
result = Math.max(result, curPos[0] * curPos[0] + curPos[1] * curPos[1]);
}
// Return the maximum distance squared the robot can be from the origin
return result;
}
}




