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
- 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.
- 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.
- 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.