Problem · Matrix
EasyIBMINTERNOA
See IBM hiring insights

Problem statement

A Mars rover is directed to move within a square matrix. It accepts a sequence of commands to move in any of the four directions from each cell: [UP, DOWN, LEFT or RIGHT]. The rover starts from cell 0. and may not move diagonally or outside of the boundary.

Each cell in the matrix has a position equal to:

(row * size) + column

where row and column are zero-indexed, size = row length of the matrix.

Return the final position of the rover after all moves.

Function

roverMove(n: int, cmds: String[]) → int

Complete the function roverMove in the editor.

roverMove has the following parameter(s):

  1. 1. int n: the size of the square matrix
  2. 2. String[] cmds: the commands

Returns

int: the label of the cell the rover occupies after executing all commands

Examples

Example 1

n = 4cmds = ["RIGHT", "UP", "DOWN", "LEFT", "DOWN", "DOWN"]return = 12
Example 1 illustration
The function returns 12.

Constraints

  • 2 ≤ n ≤ 20
  • 1 ≤ |cmds| ≤ 20

More IBM problems

drafts saved locally
public int roverMove(int n, String[] cmds) {
  // write your code here
}
n4
cmds["RIGHT", "UP", "DOWN", "LEFT", "DOWN", "DOWN"]
expected12
checking account