Quick Overview

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.

Simulate robot moves on a grid

Company: Shopify

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

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.

Quick Answer: 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.

Return the final robot position after U/D/L/R commands. Moves outside the grid or into optional blocked cells are ignored.

Constraints

  • 0-indexed coordinates
  • blocked is optional

Examples

Input: (3, 3, (1, 1), 'UURDDL')

Expected Output: [2, 1]

Input: (1, 1, (0, 0), 'UDLR')

Expected Output: [0, 0]

Hints

  1. Check bounds before committing a move.

Loading coding console...