Ctrl K

Robot on a Numbered Grid

Easy
A grid has size × size cells, numbered row by row from top to bottom and left to right: the cell in row r and column c has number r * size + c. A robot starts in cell 0, the top-left corner. The robot carries out the commands in moves one by one: "UP" steps up, "DOWN" steps down, "LEFT" steps left and "RIGHT" steps right. It is guaranteed never to leave the grid. Return the number of the cell where the robot ends up.
Example 1
size=3, moves=['RIGHT', 'RIGHT', 'DOWN'] → 5

From cell 0 the robot goes to 1, then 2, then one row down to cell 5.

Example 2
size=4, moves=['DOWN', 'DOWN', 'RIGHT', 'UP', 'LEFT'] → 4

The path is 0 → 4 → 8 → 9 → 5 → 4.

Constraints
  • 2 ≤ size ≤ 10
  • 1 ≤ len(moves) ≤ 100
  • each command is "UP", "DOWN", "LEFT" or "RIGHT"
  • the robot never leaves the grid
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA One-Variable Language EasyMirrored Codes EasyJustify the Lines HardWords Read Down the Columns MediumSimultaneous Chess Moves Hard
loading Python…
Input
size=3, moves=['RIGHT', 'RIGHT', 'DOWN']
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 64 tests on the server, hidden ones included.