Read a matrix in zigzag diagonal order

Quick Overview

Return every element of an m x n matrix by walking its anti-diagonals from the top-left corner and switching direction on each one. It tests index arithmetic on grids, careful handling of non-square shapes, and a linear traversal with constant extra space.

Read a matrix in zigzag diagonal order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an `m x n` matrix `mat`, return an array of all the elements of the matrix in zigzag diagonal order. Start at the top-left cell and walk the anti-diagonals one after another, switching direction on each one, until you reach the bottom-right cell. ### Function Signature ```python def find_diagonal_order(mat: List[List[int]]) -> List[int]: ``` ### Rules - `mat` is a list of `m` rows, each a list of `n` integers. - Diagonal `d` is the set of cells `(i, j)` with `i + j == d`, for `d = 0, 1, ..., m + n - 2`. Diagonals are visited in increasing order of `d`. - When `d` is even, read the diagonal moving up and to the right (row index decreasing). When `d` is odd, read it moving down and to the left (row index increasing). ### Constraints - `m == mat.length` - `n == mat[i].length` - `1 <= m, n <= 10^4` - `1 <= m * n <= 10^4` - `-10^5 <= mat[i][j] <= 10^5` ### Examples ```text Input: mat = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,4,7,5,3,6,8,9] ``` The diagonals in order are `[1]`, `[2,4]`, `[3,5,7]`, `[6,8]` and `[9]`. Even diagonals 0, 2 and 4 are read upward, so `[3,5,7]` becomes `7, 5, 3`, while odd diagonals 1 and 3 are read downward. ```text Input: mat = [[1,2],[3,4]] Output: [1,2,3,4] ``` The diagonals are `[1]`, `[2,3]` and `[4]`. The middle one is odd, so it is read downward from 2 to 3. ```text Input: mat = [[1,2,3],[4,5,6]] Output: [1,2,4,5,3,6] ``` In this 2 x 3 matrix the diagonals are `[1]`, `[2,4]`, `[3,5]` and `[6]`. Diagonal 2 is even, so it is read upward from 5 (row 1) to 3 (row 0).

Overview: Return every element of an m x n matrix by walking its anti-diagonals from the top-left corner and switching direction on each one. It tests index arithmetic on grids, careful handling of non-square shapes, and a linear traversal with constant extra space.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an m x n matrix mat, return an array of all the elements of the matrix in zigzag diagonal order. Start at the top-left cell and walk the anti-diagonals one after another, switching direction on each one, until you reach the bottom-right cell.

Function Signature

def find_diagonal_order(mat: List[List[int]]) -> List[int]:

Rules

  • mat is a list of m rows, each a list of n integers.
  • Diagonal d is the set of cells (i, j) with i + j == d , for d = 0, 1, ..., m + n - 2 . Diagonals are visited in increasing order of d .
  • When d is even, read the diagonal moving up and to the right (row index decreasing). When d is odd, read it moving down and to the left (row index increasing).

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 10^4
  • 1 <= m * n <= 10^4
  • -10^5 <= mat[i][j] <= 10^5

Examples

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

The diagonals in order are [1], [2,4], [3,5,7], [6,8] and [9]. Even diagonals 0, 2 and 4 are read upward, so [3,5,7] becomes 7, 5, 3, while odd diagonals 1 and 3 are read downward.

Input: mat = [[1,2],[3,4]]
Output: [1,2,3,4]

The diagonals are [1], [2,3] and [4]. The middle one is odd, so it is read downward from 2 to 3.

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

In this 2 x 3 matrix the diagonals are [1], [2,4], [3,5] and [6]. Diagonal 2 is even, so it is read upward from 5 (row 1) to 3 (row 0).