Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #766: Toeplitz Matrix

In this guide, we solve Leetcode #766 Toeplitz Matrix in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.

Quick Facts

  • Difficulty: Easy
  • Premium: No
  • Tags: Array, Matrix

Intuition

Grid problems are easiest when you define clear row/column boundaries.

A consistent traversal order prevents off-by-one errors.

Approach

Iterate by rows, columns, or layers depending on the requirement.

Keep bounds updated as the traversal progresses.

Steps:

  • Define bounds or directions.
  • Visit cells in order.
  • Update result and move bounds.

Example

Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] Output: true Explanation: In the above grid, the diagonals are: "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". In each diagonal all elements are the same, so the answer is True.

Python Solution

class Solution: def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: m, n = len(matrix), len(matrix[0]) for i in range(1, m): for j in range(1, n): if matrix[i][j] != matrix[i - 1][j - 1]: return False return True

Complexity

The time complexity is O(m×n)O(m \times n)O(m×n), where mmm and nnn are the number of rows and columns of the matrix, respectively. The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy