Master Matrix Patterns for DSA + Competitive Programming
Matrix problems involve traversing, transforming, and searching 2D grids.
Pattern Table
| Pattern | Typical Questions | Trigger |
|---|---|---|
| Traversal | Row/column/diagonal traversal | Iterate in specific order |
| Spiral | Spiral order matrix | Clockwise/counter-clockwise |
| Rotation | Rotate image, transpose | In-place transformation |
| Simulation | Game of Life, valid sudoku | Cell-by-cell rules |
| Flood Fill | Paint bucket, island fill | BFS/DFS on grid |
Mental Trigger
2D grid → Traverse in order → Transform → Done.
Generic Java Matrix Template (Base)
int n = matrix.length, m = matrix[0].length;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
// process matrix[i][j]
Recognition Cheat Sheet
| If you see… | Think… |
|---|---|
| Spiral order output | Four boundaries |
| Rotate/flip in-place | Transpose + reverse |
| Cell simulation | Check 8 neighbors |
| Flood/paint fill | BFS/DFS on grid |
Premium Content
Unlock Matrix Patterns and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans