Loading…
Loading…
Charting your lesson…
Opening your lesson…
Matrix Traversal and Operations
Lesson 8 of 8
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
A matrix is a 2-D array, and interviewers use it to test index discipline: rotating in place, spiral traversal, transposing, and searching a sorted grid. Master the coordinate bookkeeping and these become mechanical.
Core idea: an m×n matrix is stored as rows of contiguous elements; M[r][c] is O(1) access. Most matrix problems are careful bounded loops over (row, col) pairs, so the whole art is managing boundaries without going out of range.
Step 2/ 61 min
A full traversal visits m·n cells → O(m·n) time. The transpose swaps M[r][c] with M[c][r]; done in place on a square matrix it needs only the cells above the diagonal:
for r in 0..n-1:
for c in r+1..n-1:
swap(M[r][c], M[c][r])
O(n²) time, O(1) space.
Rotating a square matrix clockwise = transpose, then reverse each row:
transpose(M)
for each row: reverse(row)
Both steps are O(n²) time with O(1) extra space. (Anticlockwise = transpose then reverse each column, or reverse rows first then transpose.)
Spiral order walks the outer ring, then shrinks the boundary. Keep four bounds top, bottom, left, right; after traversing each edge, move that boundary inward and check top <= bottom && left <= right before the next edge. O(m·n) time.
Search a row-and-column-sorted matrix (each row ascends, each column ascends): start at the top-right corner. If the target is smaller, move left; if larger, move down. Each step eliminates a full row or column → O(m + n) time, far better than O(m·n) scanning or even O(m log n) per-row binary search.
Step 3/ 61 min
Rotate [[1,2,3],[4,5,6],[7,8,9]] 90° clockwise.
Transpose → [[1,4,7],[2,5,8],[3,6,9]]. Reverse each row → [[7,4,1],[8,5,2],[9,6,3]]. That is exactly the input rotated a quarter-turn clockwise, with no second matrix allocated.
Step 4/ 61 min
Step 5/ 61 min
m from column-count n; mixing them causes out-of-bounds on non-square grids.Transposing a square matrix by looping c from 0 (not r+1). Swapping every pair twice returns the matrix to its original state. Only iterate the upper triangle (c > r) so each pair is swapped exactly once.
Step 6/ 6· Worked examples1 min
Problem
Give the spiral order traversal of the matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]].
Try it on paper first.
Recall
Keep this
Usage: rotate a square matrix by transposing then reversing rows for O(1) extra space.
2D matrices appear in grid, image, and dynamic-programming problems. Interviews test index arithmetic (row/column mapping), in-place transformations, and non-linear traversal orders like spiral and diagonal.
An m×n matrix is just rows of arrays; element (r, c) is reached in O(1). Key operations:
| Operation | Time | Space |
|---|---|---|
| Element access (r,c) | O(1) | O(1) |
| Full traversal | O(m·n) | O(1) |
| Rotate 90° in place | O(m·n) | O(1) |
| Spiral order | O(m·n) | O(1) |
| Search row+col sorted | O(m+n) | O(1) |
Rotate an image, spiral/diagonal print, set zeroes, search a sorted matrix, or flood-fill/island counting on a grid. Interviewers check clean boundary handling and correct in-place transforms.
Matrix = rows of arrays, O(1) access, O(m·n) traversal. Rotate via transpose + row-reverse in place; spiral via four shrinking boundaries; search a sorted matrix from the top-right in O(m+n). Watch rows-vs-columns on non-square grids.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.