Simulate Obstacle-Aware Robot Moves and Track the Farthest Point
Company: Shopify
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
A robot starts at `(0,0)` facing north on an unbounded integer grid. Commands `-2` and `-1` turn90 degrees left and right. A command1 through9 advances that many unit steps, stopping the current forward command immediately before any obstacle. Return the maximum squared distance `x*x+y*y` reached anywhere on the path. North/east/south/west mean +y/+x/-y/-x. An obstacle at the origin does not prevent the initial departure but prevents entering the origin later.
```cpp
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles);
};
```
### Examples
**Example 1:**
```text
Input: commands = [3,-1,2], obstacles = []
Output: 13
Explanation: The path reaches(0,3), then(2,3), whose squared distance is4+9=13.
```
**Example 2:**
```text
Input: commands = [3,-1,-1,3], obstacles = [[0,0]]
Output: 9
Explanation: After reaching(0,3), the robot faces south and stops at(0,1) before the blocked origin. Its maximum remains9.
```
### Constraints
- `1 <= commands.length <= 10^4`.
- Each command is `-2`, `-1`, or an integer from1 through9.
- `0 <= obstacles.length <= 10^4`.
- Obstacle coordinates are between `-3*10^4` and `3*10^4`, inclusive.
- The answer is guaranteed smaller than `2^31`.
### Contract considerations
An origin obstacle is handled by testing destinations only: the initial position is allowed, but reentering it is blocked.
```hint Read the boundary condition
What happens when an obstacle lies halfway through a forward command?
```
### Boundary reminders
An origin obstacle is handled by testing destinations only: the initial position is allowed, but reentering it is blocked.
Overview: Simulate Obstacle-Aware Robot Moves and Track the Farthest Point. A robot starts at (0,0) facing north on an unbounded integer grid.
Simulate Obstacle-Aware Robot Moves and Track the Farthest Point
Shopify
Jun 26, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
A robot starts at (0,0) facing north on an unbounded integer grid. Commands -2 and -1 turn90 degrees left and right. A command1 through9 advances that many unit steps, stopping the current forward command immediately before any obstacle. Return the maximum squared distance x*x+y*y reached anywhere on the path. North/east/south/west mean +y/+x/-y/-x. An obstacle at the origin does not prevent the initial departure but prevents entering the origin later.
class Solution {
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles);
};
Input: commands = [3,-1,-1,3], obstacles = [[0,0]]
Output: 9
Explanation: After reaching(0,3), the robot faces south and stops at(0,1) before the blocked origin. Its maximum remains9.
Constraints
1 <= commands.length <= 10^4
.
Each command is
-2
,
-1
, or an integer from1 through9.
0 <= obstacles.length <= 10^4
.
Obstacle coordinates are between
-3*10^4
and
3*10^4
, inclusive.
The answer is guaranteed smaller than
2^31
.
Contract considerations
An origin obstacle is handled by testing destinations only: the initial position is allowed, but reentering it is blocked.
Boundary reminders
An origin obstacle is handled by testing destinations only: the initial position is allowed, but reentering it is blocked.