Watch 10 video solutions for Spiral Matrix, a medium level problem involving Array, Matrix, Simulation. This walkthrough by take U forward has 308,263 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an m x n matrix, return all elements of the matrix in spiral order.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100Problem Overview: Given an m x n matrix, return all elements of the matrix in spiral order. Start at the top-left corner, traverse the top row, then the right column, the bottom row in reverse, and the left column upward. Continue shrinking the boundaries until every element is visited exactly once.
Approach 1: Iterative Boundary Management (Time: O(m*n), Space: O(1))
This approach simulates the spiral traversal by maintaining four boundaries: top, bottom, left, and right. You iterate across the top row from left to right, then move the top boundary down. Next iterate down the right column and move the right boundary left. Continue with the bottom row (right to left) and the left column (bottom to top), shrinking the boundaries after each pass. The key insight is that each layer of the matrix forms a rectangle that can be processed in four directional passes. Since every element is visited exactly once, the time complexity is O(m*n), and only a few integer pointers are used, giving O(1) extra space.
This is primarily a simulation problem on a matrix. The algorithm directly models the traversal path rather than transforming the data. Boundary checks after each traversal prevent duplicate visits when the matrix collapses to a single row or column.
Approach 2: Recursive Divide and Conquer (Time: O(m*n), Space: O(min(m,n)))
The recursive approach processes the matrix layer by layer. Each recursive call reads the outer perimeter: traverse the top row, right column, bottom row in reverse, and left column upward. After completing the perimeter, recursively call the same logic on the inner submatrix defined by top+1, bottom-1, left+1, and right-1. Each recursion removes one rectangular layer until the boundaries cross.
The time complexity remains O(m*n) because every element is still visited once. The difference is the recursion stack, which can grow up to the number of spiral layers, roughly min(m,n)/2. That results in O(min(m,n)) auxiliary space due to recursion. This version is shorter and expressive in languages like Python, but iterative logic is usually preferred in production code.
Both approaches rely on careful index control within an array-backed grid. Edge cases occur when the remaining region collapses into a single row or column, so boundary checks must run after every directional traversal.
Recommended for interviews: Iterative boundary management is the expected solution. Interviewers want to see that you can simulate matrix traversal using pointer boundaries without extra memory. The recursive version demonstrates clean thinking about layers, but the iterative approach shows stronger control over indices and edge cases.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Boundary Management | O(m*n) | O(1) | Best general solution for interviews and production. Uses constant memory and straightforward boundary pointers. |
| Recursive Divide and Conquer | O(m*n) | O(min(m,n)) | Useful when expressing the problem in layers recursively. Cleaner in Python but adds recursion stack overhead. |