Sort every down-right diagonal of a matrix in ascending order

Quick Overview

Sort the values on every top-left to bottom-right diagonal of an m by n integer matrix in ascending order and return the matrix. It tests how you identify which cells share a diagonal, how you collect and write back each diagonal, and how you reason about time and extra space.

Sort every down-right diagonal of a matrix in ascending order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A matrix diagonal is the set of cells that starts at any cell in the top row or the leftmost column and runs down and to the right until it leaves the matrix. For example, in a matrix with at least 5 rows and 3 columns, the diagonal that starts at `mat[2][0]` contains `mat[2][0]`, `mat[3][1]` and `mat[4][2]`. Given an `m x n` matrix `mat` of integers, sort the values on each diagonal in ascending order and return the resulting matrix. ### Function Signature ```python def diagonal_sort(mat: list[list[int]]) -> list[list[int]]: ``` ### Rules - `mat` is a list of `m` rows, each a list of `n` integers. - Ascending means the smallest value of a diagonal goes to its top-left cell and the largest to its bottom-right cell. - Values never move from one diagonal to another. Duplicates stay on their diagonal. - You may modify `mat` in place and return it, or return a new matrix with the same shape. ### Constraints - `m == len(mat)` - `n == len(mat[i])` - `1 <= m, n <= 100` - `1 <= mat[i][j] <= 100` ### Examples ```text Input: mat = [[9, 4, 7], [2, 5, 1], [8, 3, 6]] Output: [[5, 1, 7], [2, 6, 4], [8, 3, 9]] ``` The main diagonal holds 9, 5, 6 and becomes 5, 6, 9. The diagonal starting at `mat[0][1]` holds 4, 1 and becomes 1, 4. The diagonal starting at `mat[1][0]` holds 2, 3, which is already ascending, and the two corner cells are diagonals of one cell each. ```text Input: mat = [[12, 16, 23, 1, 33, 8], [15, 45, 21, 50, 12, 56], [70, 30, 40, 50, 60, 12], [25, 35, 40, 30, 75, 10], [90, 32, 18, 12, 6, 55]] Output: [[ 6, 16, 10, 1, 33, 8], [12, 12, 21, 23, 12, 56], [18, 15, 30, 50, 50, 12], [25, 35, 30, 40, 55, 60], [90, 32, 70, 40, 45, 75]] ``` For instance, the main diagonal 12, 45, 40, 30, 6 becomes 6, 12, 30, 40, 45, and the diagonal starting at `mat[0][2]` (23, 50, 60, 10) becomes 10, 23, 50, 60. The other diagonals are sorted the same way. ```text Input: mat = [[4, 5, 2, 6], [3, 7, 8, 9], [1, 5, 9, 10]] Output: [[4, 5, 2, 6], [3, 7, 8, 9], [1, 5, 9, 10]] ``` Every diagonal is already ascending (4, 7, 9; 5, 8, 10; 2, 9; 3, 5; and the single cells), so the matrix does not change.

Overview: Sort the values on every top-left to bottom-right diagonal of an m by n integer matrix in ascending order and return the matrix. It tests how you identify which cells share a diagonal, how you collect and write back each diagonal, and how you reason about time and extra space.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A matrix diagonal is the set of cells that starts at any cell in the top row or the leftmost column and runs down and to the right until it leaves the matrix. For example, in a matrix with at least 5 rows and 3 columns, the diagonal that starts at mat[2][0] contains mat[2][0], mat[3][1] and mat[4][2].

Given an m x n matrix mat of integers, sort the values on each diagonal in ascending order and return the resulting matrix.

Function Signature

def diagonal_sort(mat: list[list[int]]) -> list[list[int]]:

Rules

  • mat is a list of m rows, each a list of n integers.
  • Ascending means the smallest value of a diagonal goes to its top-left cell and the largest to its bottom-right cell.
  • Values never move from one diagonal to another. Duplicates stay on their diagonal.
  • You may modify mat in place and return it, or return a new matrix with the same shape.

Constraints

  • m == len(mat)
  • n == len(mat[i])
  • 1 <= m, n <= 100
  • 1 <= mat[i][j] <= 100

Examples

Input: mat = [[9, 4, 7],
              [2, 5, 1],
              [8, 3, 6]]
Output: [[5, 1, 7],
         [2, 6, 4],
         [8, 3, 9]]

The main diagonal holds 9, 5, 6 and becomes 5, 6, 9. The diagonal starting at mat[0][1] holds 4, 1 and becomes 1, 4. The diagonal starting at mat[1][0] holds 2, 3, which is already ascending, and the two corner cells are diagonals of one cell each.

Input: mat = [[12, 16, 23,  1, 33,  8],
              [15, 45, 21, 50, 12, 56],
              [70, 30, 40, 50, 60, 12],
              [25, 35, 40, 30, 75, 10],
              [90, 32, 18, 12,  6, 55]]
Output: [[ 6, 16, 10,  1, 33,  8],
         [12, 12, 21, 23, 12, 56],
         [18, 15, 30, 50, 50, 12],
         [25, 35, 30, 40, 55, 60],
         [90, 32, 70, 40, 45, 75]]

For instance, the main diagonal 12, 45, 40, 30, 6 becomes 6, 12, 30, 40, 45, and the diagonal starting at mat[0][2] (23, 50, 60, 10) becomes 10, 23, 50, 60. The other diagonals are sorted the same way.

Input: mat = [[4, 5, 2,  6],
              [3, 7, 8,  9],
              [1, 5, 9, 10]]
Output: [[4, 5, 2,  6],
         [3, 7, 8,  9],
         [1, 5, 9, 10]]

Every diagonal is already ascending (4, 7, 9; 5, 8, 10; 2, 9; 3, 5; and the single cells), so the matrix does not change.