cswork / libraryOpen practice workspace

Coding questions · Shopify

Simulate robot moves on a grid

This question evaluates competency in grid-based simulation, state updates, boundary condition handling, and sequential command parsing. Commonly asked in Coding & Algorithms interviews to gauge correct implementation and edge-case reasoning, it assesses practical application more than abstract theory and can be extended to path reconstruction or obstacle-aware movement as follow-ups.

You are given an m x n grid and a robot that starts at position (r, c) (0-indexed). You are also given a string commands consisting of characters 'U', 'D', 'L', 'R', where each character requests moving the robot one cell up/down/left/right.

Rules:

  • If a move would take the robot outside the grid, ignore that move (the robot stays in place).

Return the robot’s final position (r, c) after processing all commands.

Follow-ups (if time): return the full path; handle blocked cells where moves into obstacles are ignored.

Simulate Robot Moves on a Grid

Constraints

  • 0-indexed coordinates
  • blocked is optional

Examples

Example 1

Input: (3, 3, (1, 1), 'UURDDL')
Output: [2, 1]

Example 2

Input: (1, 1, (0, 0), 'UDLR')
Output: [0, 0]

Hints

  1. Check bounds before committing a move.