Quick Overview

A matrix is made of 4x4 blocks that each hide one number from 1 to 16 behind a zero. Find every block's missing value, sort the blocks by it, and reassemble the grid in row-major order. Tests careful 2D index arithmetic, finding a missing number quickly, and sorting records by a computed key.

Sort the 4x4 blocks of a matrix by each block's missing value

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an `m x n` integer matrix where `m` and `n` are both multiples of 4. The matrix is a grid of non-overlapping 4x4 blocks. Each block contains 15 distinct numbers from 1 to 16, and the one number missing from the block is replaced by `0`. Find the missing value of every block, sort the blocks in ascending order of their missing values, and place the sorted blocks back into the grid in row-major order (left to right along a row of blocks, then top to bottom). Return the reassembled matrix. ### Function Signature ```python def sort_matrix_blocks(matrix: list[list[int]]) -> list[list[int]]: ... ``` ### Rules - The block in block row `br` and block column `bc` covers rows `4*br` to `4*br + 3` and columns `4*bc` to `4*bc + 3` of the matrix. - Block positions are numbered in row-major order. With `n // 4` blocks per row, position `k` is block row `k // (n // 4)` and block column `k % (n // 4)`. - The block with the smallest missing value goes to position 0 (the top-left), the block with the next smallest goes to position 1, and so on. - A block moves as a unit and its 16 cells keep their layout. The `0` stays where it is inside its block and is not replaced by the missing value. - Return a matrix with the same dimensions `m x n`. Because all missing values are distinct, the result is unique. ### Constraints - `4 <= m, n <= 40` - `m` and `n` are multiples of 4 - Each 4x4 block contains exactly 15 distinct numbers from 1 to 16 and one `0` - All missing values across blocks are distinct - Since each missing value lies in 1 to 16 and no two are equal, a valid input has at most 16 blocks: `(m // 4) * (n // 4) <= 16` ### Examples Example 1: ```text Input: matrix = [ [1, 2, 3, 4, 16, 15, 14, 13], [5, 6, 7, 8, 12, 11, 10, 9], [9, 10, 11, 12, 8, 7, 6, 5], [13, 14, 0, 16, 4, 3, 0, 1] ] Output: [ [16, 15, 14, 13, 1, 2, 3, 4], [12, 11, 10, 9, 5, 6, 7, 8], [8, 7, 6, 5, 9, 10, 11, 12], [4, 3, 0, 1, 13, 14, 0, 16] ] ``` The left block contains every number from 1 to 16 except 15, and the right block is missing 2. The right block has the smaller missing value, so it takes the left position and the original left block moves to the right. Example 2: ```text Input: matrix = [ [0, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] Output: [ [0, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] ``` The only block is missing 1. With one block there is nothing to reorder, so the output equals the input. Example 3: ```text Input: matrix = [ [1, 2, 3, 4, 16, 15, 14, 13], [5, 6, 7, 8, 12, 11, 10, 9], [0, 10, 11, 12, 8, 7, 6, 5], [13, 14, 15, 16, 4, 0, 2, 1], [1, 5, 9, 13, 0, 2, 3, 4], [2, 6, 10, 14, 5, 6, 7, 8], [3, 7, 11, 15, 9, 10, 11, 12], [4, 8, 0, 16, 13, 14, 15, 16] ] Output: [ [0, 2, 3, 4, 16, 15, 14, 13], [5, 6, 7, 8, 12, 11, 10, 9], [9, 10, 11, 12, 8, 7, 6, 5], [13, 14, 15, 16, 4, 0, 2, 1], [1, 2, 3, 4, 1, 5, 9, 13], [5, 6, 7, 8, 2, 6, 10, 14], [0, 10, 11, 12, 3, 7, 11, 15], [13, 14, 15, 16, 4, 8, 0, 16] ] ``` In row-major order the four blocks are missing 9 (top-left), 3 (top-right), 12 (bottom-left) and 1 (bottom-right). The ascending order is 1, 3, 9, 12, so the old bottom-right block moves to the top-left, the top-right block stays where it is, the old top-left block moves to the bottom-left, and the old bottom-left block moves to the bottom-right.

Overview: A matrix is made of 4x4 blocks that each hide one number from 1 to 16 behind a zero. Find every block's missing value, sort the blocks by it, and reassemble the grid in row-major order. Tests careful 2D index arithmetic, finding a missing number quickly, and sorting records by a computed key.

You are given an `m x n` integer matrix `matrix`, where `m` and `n` are both multiples of 4. The matrix is a grid of non-overlapping 4x4 blocks. Each block contains 15 distinct numbers from 1 to 16, and the one number from 1 to 16 that is missing from the block is replaced by `0`. Find the missing value of every block, sort the blocks in ascending order of their missing values, and place the sorted blocks back into the grid in row-major order (left to right along a row of blocks, then top to bottom). Return the reassembled matrix. ### Rules - The block in block row `br` and block column `bc` covers rows `4*br` to `4*br + 3` and columns `4*bc` to `4*bc + 3` of the matrix. - Block positions are numbered in row-major order. With `n // 4` blocks per row, position `k` is block row `k // (n // 4)` and block column `k % (n // 4)`. - The block with the smallest missing value goes to position 0 (the top-left), the block with the next smallest missing value goes to position 1, and so on. - A block moves as a unit and its 16 cells keep their layout. The `0` stays where it is inside its block and is not replaced by the missing value. - Return a matrix with the same dimensions `m x n`. All missing values are distinct, so the result is unique. Every cell value is between 0 and 16, so all values fit in a 32-bit integer. ### Example 1 ```text Input: matrix = [ [1, 2, 3, 4, 16, 15, 14, 13], [5, 6, 7, 8, 12, 11, 10, 9], [9, 10, 11, 12, 8, 7, 6, 5], [13, 14, 0, 16, 4, 3, 0, 1] ] Output: [ [16, 15, 14, 13, 1, 2, 3, 4], [12, 11, 10, 9, 5, 6, 7, 8], [8, 7, 6, 5, 9, 10, 11, 12], [4, 3, 0, 1, 13, 14, 0, 16] ] ``` The left block contains every number from 1 to 16 except 15, and the right block is missing 2. The right block has the smaller missing value, so it takes the left position and the original left block moves to the right. ### Example 2 ```text Input: matrix = [ [1, 2, 3, 4, 16, 15, 14, 13], [5, 6, 7, 8, 12, 11, 10, 9], [0, 10, 11, 12, 8, 7, 6, 5], [13, 14, 15, 16, 4, 0, 2, 1], [1, 5, 9, 13, 0, 2, 3, 4], [2, 6, 10, 14, 5, 6, 7, 8], [3, 7, 11, 15, 9, 10, 11, 12], [4, 8, 0, 16, 13, 14, 15, 16] ] Output: [ [0, 2, 3, 4, 16, 15, 14, 13], [5, 6, 7, 8, 12, 11, 10, 9], [9, 10, 11, 12, 8, 7, 6, 5], [13, 14, 15, 16, 4, 0, 2, 1], [1, 2, 3, 4, 1, 5, 9, 13], [5, 6, 7, 8, 2, 6, 10, 14], [0, 10, 11, 12, 3, 7, 11, 15], [13, 14, 15, 16, 4, 8, 0, 16] ] ``` In row-major order the four blocks are missing 9 (top-left), 3 (top-right), 12 (bottom-left) and 1 (bottom-right). The ascending order is 1, 3, 9, 12, so the old bottom-right block moves to the top-left, the top-right block stays where it is, the old top-left block moves to the bottom-left, and the old bottom-left block moves to the bottom-right. ### Constraints - `4 <= m, n <= 40` - `m` and `n` are multiples of 4 - Each 4x4 block contains exactly 15 distinct numbers from 1 to 16 and one `0` - All missing values across blocks are distinct - Since each missing value lies in 1 to 16 and no two are equal, a valid input has at most 16 blocks: `(m // 4) * (n // 4) <= 16`

Constraints

  • 4 <= m, n <= 40
  • m and n are multiples of 4
  • Each 4x4 block contains exactly 15 distinct numbers from 1 to 16 and one 0
  • All missing values across blocks are distinct
  • Since each missing value lies in 1 to 16 and no two are equal, a valid input has at most 16 blocks: (m // 4) * (n // 4) <= 16

Examples

Input: ([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]],)

Expected Output: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]]

Explanation: Minimum grid: one block missing 16 with its 0 in the bottom-right corner. A single block cannot move, so the output equals the input.

Input: ([[7, 3, 12, 1], [16, 0, 9, 4], [2, 14, 5, 11], [8, 13, 10, 15]],)

Expected Output: [[7, 3, 12, 1], [16, 0, 9, 4], [2, 14, 5, 11], [8, 13, 10, 15]]

Explanation: One scrambled block missing 6 with its 0 at an interior cell. It stays in place with its layout and its 0 unchanged.

Hints

  1. Each block holds all but one of the numbers 1 to 16. Think about a quantity you can compute from the 16 cells that reveals the absent number directly.
  2. Treat each block as one unit: first decide which original block belongs at each target position, then copy its 16 cells at the same offsets.
  3. Try your position formula on a grid that is not square, such as 2 block rows by 3 block columns. The number of blocks per row comes from n.

Sort 4x4 Matrix Blocks by Missing Value

easyRobloxSoftware Engineer

You are given an m x n integer matrix matrix, where m and n are both multiples of 4. The matrix is a grid of non-overlapping 4x4 blocks. Each block contains 15 distinct numbers from 1 to 16, and the one number from 1 to 16 that is missing from the block is replaced by 0.

Find the missing value of every block, sort the blocks in ascending order of their missing values, and place the sorted blocks back into the grid in row-major order (left to right along a row of blocks, then top to bottom). Return the reassembled matrix.

Rules

  • The block in block row br and block column bc covers rows 4*br to 4*br + 3 and columns 4*bc to 4*bc + 3 of the matrix.
  • Block positions are numbered in row-major order. With n // 4 blocks per row, position k is block row k // (n // 4) and block column k % (n // 4).
  • The block with the smallest missing value goes to position 0 (the top-left), the block with the next smallest missing value goes to position 1, and so on.
  • A block moves as a unit and its 16 cells keep their layout. The 0 stays where it is inside its block and is not replaced by the missing value.
  • Return a matrix with the same dimensions m x n. All missing values are distinct, so the result is unique.

Every cell value is between 0 and 16, so all values fit in a 32-bit integer.

Example 1

Input: matrix = [
  [1, 2, 3, 4, 16, 15, 14, 13],
  [5, 6, 7, 8, 12, 11, 10, 9],
  [9, 10, 11, 12, 8, 7, 6, 5],
  [13, 14, 0, 16, 4, 3, 0, 1]
]
Output: [
  [16, 15, 14, 13, 1, 2, 3, 4],
  [12, 11, 10, 9, 5, 6, 7, 8],
  [8, 7, 6, 5, 9, 10, 11, 12],
  [4, 3, 0, 1, 13, 14, 0, 16]
]

The left block contains every number from 1 to 16 except 15, and the right block is missing 2. The right block has the smaller missing value, so it takes the left position and the original left block moves to the right.

Example 2

Input: matrix = [
  [1, 2, 3, 4, 16, 15, 14, 13],
  [5, 6, 7, 8, 12, 11, 10, 9],
  [0, 10, 11, 12, 8, 7, 6, 5],
  [13, 14, 15, 16, 4, 0, 2, 1],
  [1, 5, 9, 13, 0, 2, 3, 4],
  [2, 6, 10, 14, 5, 6, 7, 8],
  [3, 7, 11, 15, 9, 10, 11, 12],
  [4, 8, 0, 16, 13, 14, 15, 16]
]
Output: [
  [0, 2, 3, 4, 16, 15, 14, 13],
  [5, 6, 7, 8, 12, 11, 10, 9],
  [9, 10, 11, 12, 8, 7, 6, 5],
  [13, 14, 15, 16, 4, 0, 2, 1],
  [1, 2, 3, 4, 1, 5, 9, 13],
  [5, 6, 7, 8, 2, 6, 10, 14],
  [0, 10, 11, 12, 3, 7, 11, 15],
  [13, 14, 15, 16, 4, 8, 0, 16]
]

In row-major order the four blocks are missing 9 (top-left), 3 (top-right), 12 (bottom-left) and 1 (bottom-right). The ascending order is 1, 3, 9, 12, so the old bottom-right block moves to the top-left, the top-right block stays where it is, the old top-left block moves to the bottom-left, and the old bottom-left block moves to the bottom-right.

Constraints

  • 4 <= m, n <= 40
  • m and n are multiples of 4
  • Each 4x4 block contains exactly 15 distinct numbers from 1 to 16 and one 0
  • All missing values across blocks are distinct
  • Since each missing value lies in 1 to 16 and no two are equal, a valid input has at most 16 blocks: (m // 4) * (n // 4) <= 16

Examples

Example 1
Input
([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]],)
Output
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]]
Notes
Minimum grid: one block missing 16 with its 0 in the bottom-right corner. A single block cannot move, so the output equals the input.
Example 2
Input
([[7, 3, 12, 1], [16, 0, 9, 4], [2, 14, 5, 11], [8, 13, 10, 15]],)
Output
[[7, 3, 12, 1], [16, 0, 9, 4], [2, 14, 5, 11], [8, 13, 10, 15]]
Notes
One scrambled block missing 6 with its 0 at an interior cell. It stays in place with its layout and its 0 unchanged.

Constraints

  • •4 <= m, n <= 40
  • •m and n are multiples of 4
  • •Each 4x4 block contains exactly 15 distinct numbers from 1 to 16 and one 0
  • •All missing values across blocks are distinct
  • •Since each missing value lies in 1 to 16 and no two are equal, a valid input has at most 16 blocks: (m // 4) * (n // 4) <= 16

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 sort_matrix_blocks(matrix: list[list[int]]) -> list[list[int]]:
Sample call
sort_matrix_blocks([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]])
Language
Case 1
Input
([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]],)
Expected
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 0]]
Case 2
Input
([[7, 3, 12, 1], [16, 0, 9, 4], [2, 14, 5, 11], [8, 13, 10, 15]],)
Expected
[[7, 3, 12, 1], [16, 0, 9, 4], [2, 14, 5, 11], [8, 13, 10, 15]]

Browse More Questions

Show the approach

Approach

Each block holds every number from 1 to 16 except one, plus a single 0. The numbers 1 to 16 add up to 136, so a block's missing value is 136 minus the sum of its 16 cells, and the 0 adds nothing to that sum.

The solution scans the blocks in any order and records (missing value, source block row, source block column) for each one. It sorts these records by missing value. The missing values are distinct, so the sorted order is unique and no tie-break is needed.

It then builds a new m x n matrix. For the record at sorted index k, the target block row is k // (n // 4) and the target block column is k % (n // 4). The 16 cells of the source block are copied to the same offsets inside the target block, so each block keeps its layout and its 0 exactly where it was, and the 0 is never replaced by the missing value.

Correctness: the sorted index k ranges over 0 to B - 1 where B = (m // 4) * (n // 4), and the row-major formula maps these indices one-to-one onto the B block positions. Every target position therefore receives exactly one block, the block with the k-th smallest missing value, which is the rule in the statement. Using n // 4 (blocks per row) in the formula matters on grids that are not square, such as 8 x 32 or 12 x 8.

Edge cases: a single 4x4 block maps to itself, so the output equals the input. One block row (4 x n) and one block column (m x 4) follow the same formula. The input matrix is never modified; a fresh matrix is returned.

Time complexity:
O(m * n + B log B), where B = (m // 4) * (n // 4) <= 16 is the number of blocks
Space complexity:
O(m * n) for the returned matrix