Data Structures
Matrix/Grid Fundamentals
Difficulty: Beginner
Picture an image as a grid of pixels, a chessboard as an 8 by 8 array of squares, or a maze as a rectangle of walls and corridors. Each of these is a Matrix/Grid: a two-dimensional array indexed by (row, column) where the right traversal...
Matrix/Grid Fundamentals
Picture an image as a grid of pixels, a chessboard as an 8 by 8 array of squares, or a maze as a rectangle of walls and corridors. Each of these is a Matrix/Grid: a two-dimensional array indexed by (row, column) where the right traversal order can turn a hard problem into a single nested loop.
This lesson covers how to allocate and initialize 2D arrays in JavaScript and Python, how row-major storage maps a [i][j] access to a contiguous memory offset, and the standard traversal patterns: row by row, column by column, and along the two diagonals. You will also practice boundary checks, the most common source of off-by-one bugs in grid problems, and implement a transpose to see how index swaps reshape data in place.
In Arrays & Strings, you saw that a one-dimensional array gives O(1) indexed access through a single offset calculation. A grid is just nested arrays sharing that same property along two axes, so every shifting and bounds idea you used there carries over directly.
Once you can move confidently through a grid, Linked Lists (Singly) shifts the focus to a very different memory layout: pointer-based nodes with no contiguous block at all.
Prerequisites:
Arrays & StringsTopics:
What You'll Learn
By the end of this lesson, you will be able to:
Create and initialize 2D arrays in JavaScript and Python.
Access elements using row and column indices, and explain how 2D arrays map to memory.
Traverse a matrix row-by-row, column-by-column, and diagonally.
Implement matrix transpose and identify its time and space complexity.
Apply boundary checking to avoid index-out-of-bounds errors in grid problems.
Recognize when a problem requires a grid/matrix representation.
Why This Matters
01
Many interview problems (BFS/DFS on grids, DP on grids, image rotation) operate directly on 2D arrays.
02
Matrices are the foundation for graph adjacency matrices, game boards, spreadsheets, and pixel grids.
03
Understanding row-major storage helps you write cache-friendly code that runs significantly faster in practice.
04
Grid traversal patterns (row-by-row, column-by-column, diagonal) are reusable building blocks for hundreds of problems.
Key Terms
8 terms you'll encounter in this lesson
Matrix
A two-dimensional array arranged in rows and columns, accessed by a pair of indices (row, column).
Row-major order
A memory layout where elements of the same row are stored contiguously. C, JavaScript, and Python use row-major order.
Column-major order
A memory layout where elements of the same column are stored contiguously. Fortran and MATLAB use column-major order.
Transpose
Flipping a matrix over its main diagonal, turning rows into columns and vice versa. Element at (i, j) moves to (j, i).
Main diagonal
The diagonal from the top-left corner to the bottom-right corner of a matrix, where row index equals column index (i == j).
Anti-diagonal
The diagonal from the top-right corner to the bottom-left corner, where row + column equals a constant (i + j == n - 1 for the main anti-diagonal).
Grid
A matrix used to represent a spatial layout, such as a game board, map, or image. Cells often represent positions with properties (walls, open paths, etc.).
Boundary checking
Verifying that row and column indices are within valid bounds (0 <= row < rows and 0 <= col < cols) before accessing a matrix element.
Related Problems
Coding problems that put this lesson's concepts into practice
Heads Up
Common misconceptions to watch out for
"row[i] gives me a column"
In a 2D array, matrix[i] gives you the entire row at index i. To get a column, you must iterate: [matrix[r][col] for r in range(rows)]. There is no built-in column access.
"Iterating column-by-column is just as fast as row-by-row"
In row-major languages (JS, Python, C), iterating by rows is cache-friendly because elements in the same row are adjacent in memory. Column-by-column traversal jumps across memory locations, causing cache misses and slower performance.
"Transposing a matrix is always O(1) extra space"
In-place transpose is O(1) space only for square matrices. For non-square matrices (m x n where m != n), you need a new (n x m) matrix, requiring O(m x n) extra space.
"I can use `[[0] * cols] * rows` in Python to create a 2D array"
This creates rows that all reference the same inner list. Modifying one row changes all of them. Use `[[0] * cols for _ in range(rows)]` instead, which creates independent row lists.
