Quick Overview

Rotate the integer values of a square grid k steps forward in reading order while every cell marked X stays in place. The problem tests mapping grid cells to a flat order, handling a rotation count larger than the number of values, and the edge case of a grid with no integer cells.

Rotate a matrix's integer values k steps in reading order while X cells stay fixed

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an `n x n` matrix whose cells hold either the string `"X"` or an integer written as a string, rotate only the integer values `k` times while every `"X"` stays where it is. The integer cells are ordered by reading order: left to right within a row, and rows from top to bottom. One rotation moves every integer value one position forward in that order, and the value in the last position wraps around to the first position. Return the matrix after `k` rotations. ### Function Signature ```python def rotate_non_x(matrix: list[list[str]], k: int) -> list[list[str]]: ``` ### Rules - Each cell is either the exact string `"X"` or the decimal string form of an integer, such as `"7"` or `"-12"`. Values are moved as the same strings and never changed. - `"X"` cells keep their positions. Only the integer cells take part in the rotation. - One rotation is a shift by one position forward in reading order: the value at position `i` of the reading order moves to position `i + 1`, and the last value moves to position 0. - If the matrix has no integer cells, return it unchanged. - You may modify `matrix` in place and return it, or return a new matrix with the same contents. ### Constraints - `1 <= n <= 10` - `0 <= k <= 1000` - Every cell is `"X"` or an integer in the range `[-100, 100]` ### Examples ```text Input: matrix = [["1","X","3"],["X","5","6"],["7","8","X"]], k = 2 Output: [["7","X","8"],["X","1","3"],["5","6","X"]] ``` The integer values in reading order are `[1, 3, 5, 6, 7, 8]`. Two rotations give `[7, 8, 1, 3, 5, 6]`, and writing those back into the six integer cells in reading order produces the output. ```text Input: matrix = [["X","2"],["3","X"]], k = 1 Output: [["X","3"],["2","X"]] ``` The values are `[2, 3]`. One rotation moves 2 to the second integer cell and wraps 3 around to the first. ```text Input: matrix = [["4","X"],["-1","9"]], k = 7 Output: [["9","X"],["4","-1"]] ``` There are three integer values, `[4, -1, 9]`, so every three rotations bring them back to where they started. Seven rotations act like one, giving `[9, 4, -1]`.

Overview: Rotate the integer values of a square grid k steps forward in reading order while every cell marked X stays in place. The problem tests mapping grid cells to a flat order, handling a rotation count larger than the number of values, and the edge case of a grid with no integer cells.

You are given an `n x n` matrix `matrix` of strings and a non-negative integer `k`. Each cell is either the exact string `"X"` or the decimal string form of an integer, such as `"7"` or `"-12"`. Rotate only the integer values `k` times while every `"X"` cell stays where it is. The integer cells are ordered by reading order: left to right within a row, and rows from top to bottom. One rotation moves every integer value one position forward in that order: the value at position `i` of the reading order moves to position `i + 1`, and the value in the last position wraps around to position `0`. Return the matrix after `k` rotations. ### Rules - Values are moved as the same strings and are never changed. - `"X"` cells keep their positions. Only the integer cells take part in the rotation. - If the matrix has no integer cells, return it unchanged. - You may modify `matrix` in place and return it, or return a new matrix with the same contents. ### Example 1 ```text Input: matrix = [["1","X","3"],["X","5","6"],["7","8","X"]], k = 2 Output: [["7","X","8"],["X","1","3"],["5","6","X"]] ``` The integer values in reading order are `[1, 3, 5, 6, 7, 8]`. Two rotations give `[7, 8, 1, 3, 5, 6]`, and writing those back into the six integer cells in reading order produces the output. ### Example 2 ```text Input: matrix = [["4","X"],["-1","9"]], k = 7 Output: [["9","X"],["4","-1"]] ``` There are three integer values, `[4, -1, 9]`, so every three rotations bring them back to where they started. Seven rotations act like one, giving `[9, 4, -1]`. ### Constraints - `1 <= n <= 10`, and `matrix` has `n` rows of `n` cells each - `0 <= k <= 1000` - Every cell is `"X"` or an integer in the range `[-100, 100]` written as a decimal string

Constraints

  • 1 <= n <= 10, and matrix has n rows of n cells each
  • 0 <= k <= 1000
  • Every cell is "X" or an integer in the range [-100, 100] written as a decimal string

Examples

Input: ([['1', 'X', '3'], ['X', '5', '6'], ['7', '8', 'X']], 2)

Expected Output: [['7', 'X', '8'], ['X', '1', '3'], ['5', '6', 'X']]

Explanation: Source example: values [1, 3, 5, 6, 7, 8] shifted forward twice become [7, 8, 1, 3, 5, 6]; reading order differs from column order here.

Input: ([['X', '2'], ['3', 'X']], 1)

Expected Output: [['X', '3'], ['2', 'X']]

Explanation: Source example: the two values trade places after one rotation.

Hints

  1. Since the X cells never move, it helps to think of the integer values, taken in reading order, as one circular sequence.
  2. After as many rotations as there are integer cells, every value is back where it started, which matters when k is large.
  3. Consider what should happen when the matrix has no integer cells, or only one.

Rotate Matrix Values Around Fixed X Cells

easyRobloxSoftware Engineer

You are given an n x n matrix matrix of strings and a non-negative integer k. Each cell is either the exact string "X" or the decimal string form of an integer, such as "7" or "-12".

Rotate only the integer values k times while every "X" cell stays where it is. The integer cells are ordered by reading order: left to right within a row, and rows from top to bottom. One rotation moves every integer value one position forward in that order: the value at position i of the reading order moves to position i + 1, and the value in the last position wraps around to position 0.

Return the matrix after k rotations.

Rules

  • Values are moved as the same strings and are never changed.
  • "X" cells keep their positions. Only the integer cells take part in the rotation.
  • If the matrix has no integer cells, return it unchanged.
  • You may modify matrix in place and return it, or return a new matrix with the same contents.

Example 1

Input: matrix = [["1","X","3"],["X","5","6"],["7","8","X"]], k = 2
Output: [["7","X","8"],["X","1","3"],["5","6","X"]]

The integer values in reading order are [1, 3, 5, 6, 7, 8]. Two rotations give [7, 8, 1, 3, 5, 6], and writing those back into the six integer cells in reading order produces the output.

Example 2

Input: matrix = [["4","X"],["-1","9"]], k = 7
Output: [["9","X"],["4","-1"]]

There are three integer values, [4, -1, 9], so every three rotations bring them back to where they started. Seven rotations act like one, giving [9, 4, -1].

Constraints

  • 1 <= n <= 10, and matrix has n rows of n cells each
  • 0 <= k <= 1000
  • Every cell is "X" or an integer in the range [-100, 100] written as a decimal string

Examples

Example 1
Input
([['1', 'X', '3'], ['X', '5', '6'], ['7', '8', 'X']], 2)
Output
[['7', 'X', '8'], ['X', '1', '3'], ['5', '6', 'X']]
Notes
Source example: values [1, 3, 5, 6, 7, 8] shifted forward twice become [7, 8, 1, 3, 5, 6]; reading order differs from column order here.
Example 2
Input
([['X', '2'], ['3', 'X']], 1)
Output
[['X', '3'], ['2', 'X']]
Notes
Source example: the two values trade places after one rotation.

Constraints

  • •1 <= n <= 10, and matrix has n rows of n cells each
  • •0 <= k <= 1000
  • •Every cell is "X" or an integer in the range [-100, 100] written as a decimal string

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def rotate_non_x(matrix: list[list[str]], k: int) -> list[list[str]]:
Sample call
rotate_non_x([['1', 'X', '3'], ['X', '5', '6'], ['7', '8', 'X']], 2)
Language
Case 1
Input
([['1', 'X', '3'], ['X', '5', '6'], ['7', '8', 'X']], 2)
Expected
[['7', 'X', '8'], ['X', '1', '3'], ['5', '6', 'X']]
Case 2
Input
([['X', '2'], ['3', 'X']], 1)
Expected
[['X', '3'], ['2', 'X']]

Browse More Questions

Show the approach

Approach

Scan the matrix in reading order (row by row, left to right) and record, for every cell that is not "X", its (row, column) position and its string value. Let m be the number of integer cells. If m is 0 there is nothing to rotate, so return a copy of the matrix as it is; this also avoids a modulo by zero. Otherwise one rotation sends the value at reading position i to position (i + 1) mod m, so k rotations send it to (i + k) mod m. After m rotations every value is back at its start, so only s = k mod m matters. Build a copy of the matrix and write values[i] into positions[(i + s) mod m] for every i. The map from i to (i + s) mod m is a bijection on 0 to m - 1, so each integer cell receives exactly one value, and "X" cells are never written, so they stay fixed. Values are copied as strings, so entries such as "-100" or "0" come back exactly as given. Edge cases: no integer cells (returned unchanged), a single integer cell (it maps to itself), k = 0 or a multiple of m (the identity), and k larger than m (reduced modulo m, never modulo n * n).

Time complexity:
O(n^2)
Space complexity:
O(n^2)