Restore the missing value in a 4x4 arithmetic progression matrix, then sort each 2x2 quadrant

Quick Overview

Given a 4x4 integer matrix whose rows and columns are arithmetic progressions, with one entry replaced by zero, recover the missing value and return the matrix with each 2x2 quadrant sorted in row-major order. It tests reasoning about arithmetic sequences and careful index handling on a small grid.

Restore the missing value in a 4x4 arithmetic progression matrix, then sort each 2x2 quadrant

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a 4x4 matrix of integers in which exactly one entry has been replaced by `0`. The `0` marks the missing value. In the complete matrix, every row and every column is an arithmetic progression: consecutive entries differ by a constant, and each row and each column has its own constant. Find the missing value and put it back. Then split the restored matrix into its four 2x2 quadrants (top-left, top-right, bottom-left, bottom-right), sort the four values of each quadrant in ascending order, write them back into that quadrant in row-major order, and return the resulting 4x4 matrix. ### Function Signature ```python from typing import List def restore_and_sort_quadrants(matrix: List[List[int]]) -> List[List[int]]: ... ``` ### Rules - The quadrants are rows 0-1 with columns 0-1 (top-left), rows 0-1 with columns 2-3 (top-right), rows 2-3 with columns 0-1 (bottom-left), and rows 2-3 with columns 2-3 (bottom-right). - Row-major order inside a quadrant means the smallest value goes to the quadrant's upper-left cell, the next to its upper-right cell, the next to its lower-left cell, and the largest to its lower-right cell. - Each quadrant is sorted on its own. Values never move from one quadrant to another. - A common difference may be positive, negative or zero. ### Constraints - The matrix is always 4x4. - Exactly one entry is `0`, and it marks the missing value. - Every other entry is between 1 and 100. - In the restored matrix, every row and every column is an arithmetic progression. - The missing value is a positive integer. ### Examples ```text Input: matrix = [[2,4,6,8],[4,6,8,10],[6,8,0,12],[8,10,12,14]] Output: [[2,4,6,8],[4,6,8,10],[6,8,10,12],[8,10,12,14]] ``` Row 2 reads 6, 8, ?, 12 with common difference 2, so the missing value is 10. Every quadrant of the restored matrix is already in ascending row-major order (the top-left one holds 2, 4, 4, 6), so sorting changes nothing. ```text Input: matrix = [[1,3,5,7],[0,6,8,10],[7,9,11,13],[10,12,14,16]] Output: [[1,3,5,7],[4,6,8,10],[7,9,11,13],[10,12,14,16]] ``` The gap is at the start of row 1. That row continues 6, 8, 10 with difference 2, so its first entry is 4, and column 0 (1, ?, 7, 10 with difference 3) gives the same value. All four quadrants are already sorted. ```text Input: matrix = [[20,17,14,11],[21,18,0,12],[22,19,16,13],[23,20,17,14]] Output: [[17,18,11,12],[20,21,14,15],[19,20,13,14],[22,23,16,17]] ``` Row 1 reads 21, 18, ?, 12 with difference -3, so the missing value is 15. The top-left quadrant holds 20, 17, 21, 18, which sorts to 17, 18, 20, 21 and fills that quadrant as `[17, 18]` over `[20, 21]`. The other three quadrants are sorted the same way.

Overview: Given a 4x4 integer matrix whose rows and columns are arithmetic progressions, with one entry replaced by zero, recover the missing value and return the matrix with each 2x2 quadrant sorted in row-major order. It tests reasoning about arithmetic sequences and careful index handling on a small grid.

Roblox logo
Roblox
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a 4x4 matrix of integers in which exactly one entry has been replaced by 0. The 0 marks the missing value. In the complete matrix, every row and every column is an arithmetic progression: consecutive entries differ by a constant, and each row and each column has its own constant.

Find the missing value and put it back. Then split the restored matrix into its four 2x2 quadrants (top-left, top-right, bottom-left, bottom-right), sort the four values of each quadrant in ascending order, write them back into that quadrant in row-major order, and return the resulting 4x4 matrix.

Function Signature

from typing import List

def restore_and_sort_quadrants(matrix: List[List[int]]) -> List[List[int]]:
    ...

Rules

  • The quadrants are rows 0-1 with columns 0-1 (top-left), rows 0-1 with columns 2-3 (top-right), rows 2-3 with columns 0-1 (bottom-left), and rows 2-3 with columns 2-3 (bottom-right).
  • Row-major order inside a quadrant means the smallest value goes to the quadrant's upper-left cell, the next to its upper-right cell, the next to its lower-left cell, and the largest to its lower-right cell.
  • Each quadrant is sorted on its own. Values never move from one quadrant to another.
  • A common difference may be positive, negative or zero.

Constraints

  • The matrix is always 4x4.
  • Exactly one entry is 0 , and it marks the missing value.
  • Every other entry is between 1 and 100.
  • In the restored matrix, every row and every column is an arithmetic progression.
  • The missing value is a positive integer.

Examples

Input: matrix = [[2,4,6,8],[4,6,8,10],[6,8,0,12],[8,10,12,14]]
Output: [[2,4,6,8],[4,6,8,10],[6,8,10,12],[8,10,12,14]]

Row 2 reads 6, 8, ?, 12 with common difference 2, so the missing value is 10. Every quadrant of the restored matrix is already in ascending row-major order (the top-left one holds 2, 4, 4, 6), so sorting changes nothing.

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

The gap is at the start of row 1. That row continues 6, 8, 10 with difference 2, so its first entry is 4, and column 0 (1, ?, 7, 10 with difference 3) gives the same value. All four quadrants are already sorted.

Input: matrix = [[20,17,14,11],[21,18,0,12],[22,19,16,13],[23,20,17,14]]
Output: [[17,18,11,12],[20,21,14,15],[19,20,13,14],[22,23,16,17]]

Row 1 reads 21, 18, ?, 12 with difference -3, so the missing value is 15. The top-left quadrant holds 20, 17, 21, 18, which sorts to 17, 18, 20, 21 and fills that quadrant as [17, 18] over [20, 21]. The other three quadrants are sorted the same way.