Toeplitz Matrix

EasyMatrixdiagonal scan

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A matrix is called a **Toeplitz matrix** if every diagonal that runs from the top-left toward the bottom-right contains the same value in all of its cells. Concretely, for any cell matrix[r][c] that has a cell directly above and to its left (its top-left neighbor matrix[r-1][c-1]), the two values must be equal. Given an m x n integer grid matrix, return true if the matrix is Toeplitz, and false otherwise.

Examples

Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: true
The diagonals (top-left to bottom-right) are [9], [5,5], [1,1,1], [2,2,2], [3,3], [4]. Every diagonal holds a single repeated value, so the matrix is Toeplitz.
Input: matrix = [[1,2],[2,2]]
Output: false
The main diagonal contains 1 (at matrix[0][0]) and 2 (at matrix[1][1]). Since these differ, the matrix is not Toeplitz.
Input: matrix = [[5]]
Output: true
A single cell trivially satisfies the condition since there are no top-left neighbors to compare against.

Constraints

  • `m == matrix.length`
  • `n == matrix[0].length`
  • `1 <= m, n <= 20`
  • `0 <= matrix[r][c] <= 99`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.