KatInterview

Sequential Robot Navigation on a Bounded Grid

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingsimulationstate-machinegrid-navigation

Last updated

Tests state-machine simulation with coordinate systems, directional rotation logic, boundary checking, and collision detection between sequentially deployed agents.

Problem Description

You are building a control system for autonomous robots operating on a rectangular grid floor. The grid extends from (0, 0) at the bottom-left to (width - 1, height - 1) at the top-right.

Each robot starts at a specified position (x, y) facing one of four cardinal directions: North ('N'), East ('E'), South ('S'), or West ('W'). Robots execute their instruction sequences one at a time: each robot completes all its commands before the next robot begins. After finishing, a robot remains stationary at its final position and becomes an obstacle for later robots.

Each robot receives a command string composed of:

  • 'L': Turn 90 degrees left (counterclockwise). Example: North → West.
  • 'R': Turn 90 degrees right (clockwise). Example: North → East.
  • 'M': Advance one unit forward in the current facing direction.

Movement Rules

  • If a forward move would place the robot outside the grid bounds (negative coordinates or x >= width or y >= height), the move is skipped and the robot stays in place.
  • If a forward move would place the robot on a cell occupied by a previously completed robot, the move is skipped and the robot stays in place.
  • Rotation commands always succeed.
  • After a blocked move, the robot continues executing remaining commands normally.

Your task is to determine each robot's final position and orientation after all have completed their sequences.

Input

  • width: Integer, the grid width (x-axis extent).
  • height: Integer, the grid height (y-axis extent).
  • robots: A list of tuples (x, y, facing, instructions) where:
    • x, y: Starting coordinates (integers).
    • facing: Starting direction (string: 'N', 'E', 'S', or 'W').
    • instructions: Command string (composed of 'L', 'R', 'M' characters).

Output

Return a list of tuples (final_x, final_y, final_facing) representing each robot's ending state, in the order the robots were provided.

Constraints

  • 1 ≤ width, height ≤ 1000
  • 0 ≤ len(robots) ≤ 500
  • 0 ≤ len(instructions) ≤ 10,000 for each robot
  • Starting positions satisfy 0 ≤ x < width and 0 ≤ y < height
  • No robot starts on a position occupied by a previously finished robot

Examples

Example 1

Input: navigate_robots(5, 5, [[1, 2, 'N', 'LMLMLMLMM'], [3, 3, 'E', 'MMRMMRMRRM']])

Output: [[1, 3, 'N'], [4, 1, 'E']]

Example 2

Input: navigate_robots(1, 1, [[0, 0, 'N', 'MMMMRMLLL']])

Output: [[0, 0, 'S']]

Example 3

Input: navigate_robots(5, 5, [])

Output: []

Hints

Hint 1

Represent the four cardinal directions in clockwise order so rotation becomes simple modular arithmetic on an index.

Hint 2

Precompute direction-to-movement deltas. Since (0, 0) is bottom-left, moving North increases y.

Hint 3

Track occupied cells in a set. Only add a robot's final position after it completes all commands, so it doesn't block itself.