Matrix
Матрица
A matrix arranges values in a rectangular grid, with each cell identified by a row and a column. Code often stores this grid in a two-dimensional array.
Also known as: двумерный массив
What makes a grid a matrix?
A matrix has the same number of entries in every row. Its shape is written as rows × columns, so a 2 × 3 matrix has two rows with three entries each. Those entries might be numbers, pixels, or symbols, depending on the problem. A matrix is square only when its two dimensions are equal.
In a coding problem, “matrix” describes the data, while “two-dimensional array” usually describes how it is stored. A Python list of lists is one possible representation, but it can also hold rows of different lengths. That is a jagged structure, not a matrix, and code that assumes a fixed width can fail on it.
Indexing and storage
With an array of rows, grid[r][c] selects row r and then column c within that row. In zero-based indexing, valid rows run from 0 to rows - 1, and valid columns run from 0 to cols - 1. Keep the two limits separate: len(grid) counts rows, not columns.
You do not need nested arrays. If you store the cells in one flat array, with each complete row followed by the next, the offset of (r, c) is r * cols + c. This layout is called row-major order. Both layouts allow O(1) cell access with ordinary indexed arrays. A dense grid stores every cell, so its storage and the time for a full scan are both O(rows × cols).
Example: finding neighbors
A grid can act like a graph without storing a separate list of edges. The following function yields the neighbors that share a side with a cell. It assumes the grid is nonempty and rectangular and the starting coordinates are valid.
def neighbors(grid, r, c):
rows, cols = len(grid), len(grid[0])
for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
yield nr, nc
The bounds check keeps the walk inside the grid. The lower bounds matter in Python: without them, moving up from row 0 could produce row -1, which Python interprets as the last row. Finding a cell's neighbors takes O(1) time here because there are only four candidates.
Where it comes up in interviews
Matrices show up in image processing, game boards, and dynamic programming tables. A common interview task is to count islands in a grid. A breadth-first or depth-first search can explore each island using the neighbor rule above. If you mark cells as visited when you discover them, each cell is processed at most once, so the full solution takes O(rows × cols) time. A separate visited grid uses O(rows × cols) space; marking the input instead removes that grid, but the search queue or stack can still grow.
Other tasks ask you to transpose a matrix, rotate it in place, or visit cells in spiral order. Before coding, check whether the input is guaranteed to be square or nonempty and whether you may modify it. A 2 × 3 test exposes swapped dimensions, while single-row and single-column tests catch many boundary errors.