Walking Robot Simulation

MediumArrays & Hashingsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A robot starts at (0, 0) on an infinite grid facing north (the positive y direction). It executes the integers in commands one by one: - -2: turn left 90 degrees. - -1: turn right 90 degrees. - 1 to 9: move forward that many unit steps, one step at a time. Some cells are blocked; obstacles[i] = [x, y] marks one. If the next step would enter an obstacle, the robot stays where it is and skips the rest of that move command, then continues with the next command. No obstacle is ever at (0, 0). Return the largest squared Euclidean distance x^2 + y^2 from the origin that the robot reaches at any moment.

Examples

Input: commands = [4,-1,3], obstacles = []
Output: 25
The robot walks to (0,4), turns to face east, and walks to (3,4). 3^2 + 4^2 = 25.
Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output: 65
After reaching (0,4) and turning east, the robot is blocked at (1,4). It turns north and walks to (1,8): 1 + 64 = 65.
Input: commands = [6,-1,-1,6], obstacles = []
Output: 36
The robot reaches (0,6), turns around, and walks back to the origin. The farthest point was (0,6).

Constraints

  • `1 <= commands.length <= 10^4`
  • `commands[i]` is `-2`, `-1`, or an integer in `[1, 9]`
  • `0 <= obstacles.length <= 10^4`
  • `-3 * 10^4 <= x, y <= 3 * 10^4`
  • No obstacle is located at `(0, 0)`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems