A matrix is Toeplitz when every diagonal running from top-left to bottom-right holds a single value. The question looks like a warm-up, but interviewers like it because the follow-ups test whether you understand what your loop actually reads.
Every cell that is not in the first row or first column must equal the cell up and to the left of it. If that holds everywhere, every diagonal is constant, because each diagonal is a chain of these pairs.
def is_toeplitz(matrix):
for r in range(1, len(matrix)):
for c in range(1, len(matrix[0])):
if matrix[r][c] != matrix[r - 1][c - 1]:
return False
return True
Time is O(m × n), extra space is O(1). There is no need to walk diagonals explicitly or to store a value per diagonal in a hash map, although the hash map version (key r - c) is a fine thing to mention as an alternative.
If you can only hold one row in memory, notice that the check above only ever compares row r with row r - 1. Keep the previous row, and require that row[1:] == prev[:-1]. Memory is O(n).
Split each row into column chunks that overlap by one element. Chunk k of row r is compared with chunk k of row r - 1, shifted by one. Saying this out loud shows you understand that the dependency is local, which is the real point of the question.
Diagonal Traverse and Sort the Matrix Diagonally both reuse the fact that every cell on one diagonal shares the same r - c.
Disclosure: I work on Linkjob AI. If you want help with problems like this in live coding rounds, AI help for online coding assessments is what we build.