Problem · Matrix
Rover Move
Learn this problemProblem 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.
int n: the size of the square matrix - 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
The function returns 12.
Constraints
- 2 ≤ n ≤ 20
- 1 ≤ |cmds| ≤ 20
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026