Tags

Matrix Algorithms

Matrix Algorithms

2 lessons
9 problems
5 community items

matrix-algorithms

Data Structures

1 lesson

Matrix/Grid Fundamentals

Free
Beginner

35 min

1 prereq

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.

Not Started

0%

Arrays
Data Structures
Beginner
Free
Matrix Traversal
Matrix Algorithms
Fundamentals
Time Complexity

Algorithms

1 lesson

Matrix Algorithms

Intermediate

55 min

2 prereqs

Rotating an `n x n` image 90 degrees clockwise sounds like it needs a second matrix to hold the output. The standard trick is two passes over the same matrix: transpose it (swap rows and columns), then reverse each row, and the rotation appears in place with no extra memory. Almost every matrix interview problem hides a similar two-step decomposition under what looks like a hard spatial puzzle. **Matrix Algorithms** trains those decompositions. You will implement spiral, diagonal, snake, and anti-diagonal traversals by managing the four boundary indices that change as the walk continues. You will rotate by 90, 180, and 270 degrees in place. You will search sorted matrices three ways: per-row binary search, the staircase search that runs in `O(m + n)` on row-and-column-sorted matrices, and a single binary search when the whole matrix is sorted. The lesson finishes with in-place transformations like "set matrix zeroes" and Conway's Game of Life that encode state inside the matrix itself. In **Matrix/Grid Fundamentals**, you learned how 2D arrays are laid out and indexed. **Iteration Patterns on Arrays/Strings** taught you single and nested loops in 1D; matrix algorithms layer those patterns across rows and columns with careful boundary management. From here, **Dynamic Programming (Advanced)** revisits 2D structures, this time as DP tables.

Not Started

0%

Algorithms
Matrix Algorithms
Matrix Traversal
Spiral Order
Rotate Matrix
Intermediate
Premium

Practice Problems

9 problems

Construct Quad Tree

Not Started
Medium

Given an n x n binary matrix, construct a Quad-Tree representation by recursively partitioning the grid into four equal quadrants.

Divide and Conquer
Recursion
Matrix Algorithms
Trees
Intermediate

952

5

Game of Life

Not Started
Medium

Implement Conway's Game of Life: compute the next state of a board in-place using state-encoding to avoid extra space.

Arrays
Matrix Algorithms
Matrix Traversal
Simulation
In-Place
Intermediate

489

5

Minimum Path Sum

Not Started
Medium

Given an m x n grid filled with non-negative numbers, find a path from the top-left to the bottom-right that minimizes the sum of all numbers along the path.

Dynamic Programming
Tabulation
Grid DP
Matrix Algorithms
Algorithms
Intermediate

841

24

Rotate Image

Free
Not Started
Medium

Rotate an n x n 2D matrix by 90 degrees clockwise in-place, without allocating another matrix.

Arrays
Matrix Algorithms
Matrix Traversal
Rotate Matrix
In-Place
Intermediate

850

20

Set Matrix Zeroes

Free
Not Started
Medium

Given an m x n integer matrix, if an element is 0, set its entire row and column to 0 using constant extra space.

Arrays
Matrix Algorithms
Matrix Traversal
In-Place
Hash Map / Dictionary
Intermediate

861

14

Spiral Matrix

Free
Not Started
Medium

Return all elements of an m x n matrix in spiral order, traversing from the outer boundary inward.

Arrays
Matrix Algorithms
Matrix Traversal
Spiral Order
Simulation
Intermediate

364

10

Unique Paths II

Not Started
Medium

Given an m x n grid with obstacles, count the number of unique paths from the top-left corner to the bottom-right corner, moving only right or down.

Dynamic Programming
Tabulation
Grid DP
Matrix Algorithms
Algorithms
Intermediate

998

27

Valid Sudoku

Not Started
Medium

Determine if a 9x9 Sudoku board is valid by checking rows, columns, and 3x3 sub-boxes for duplicate digits using hash sets.

Arrays
Hash Map / Dictionary
Set
Matrix Algorithms
Intermediate

1k

24

01 Matrix

Not Started
Medium

Given a binary matrix, find the distance of each cell to the nearest 0 using multi-source BFS.

Arrays
Matrix Algorithms
Matrix Traversal
BFS
Multi-Source BFS
Intermediate

602

4