Spiral Matrix is the #18 most asked LeetCode problem globally ā the standard matrix simulation problem that tests your ability to manage boundary conditions cleanly under pressure.
Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 60 times across 24 companies ā including Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, Databricks, DE Shaw, CrowdStrike, J.P. Morgan, and Capital One.
There are two clean approaches: boundary shrinking (track top/bottom/left/right walls and peel the matrix layer by layer) and direction vector (walk in a direction until you hit a wall or visited cell, then turn right). Boundary shrinking is more predictable and easier to explain in interviews.
| Metric | Value |
|---|---|
| Total Interview Appearances | 60 verified mentions |
| Companies That Ask It | 24 companies |
| Global Rank | #18 out of 10,385 questions |
| Difficulty | Medium |
| Core Patterns | Array, Matrix, Simulation |
Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, Databricks, DE Shaw, CrowdStrike, J.P. Morgan, Capital One, AMD, Adobe, Anduril, Apple, Cisco, Deutsche Bank, Epic Systems, IBM, Infosys, Intuit, Lenskart, and 2 more.
LeetCode #54 ā Spiral Matrix
Given an m x n matrix, return all elements of the matrix in spiral order.
Input:
matrix = [[1,2,3],
[4,5,6],
[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
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]
Maintain four boundary pointers: top, bottom, left, right. On each pass, traverse the current outer layer in four directions ā left-to-right along top, top-to-bottom along right, right-to-left along bottom, bottom-to-top along left ā then shrink each boundary inward by 1. Repeat until boundaries cross.
top row, then top++right column, then right--bottom row (if top <= bottom), then bottom--left column (if left <= right), then left++The guards on steps 3 and 4 prevent double-counting single rows or columns in the middle.
def spiralOrder(matrix: list[list[int]]) -> list[int]:
result = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while top <= bottom and left <= right:
# Left to right along top row
for col in range(left, right + 1):
result.append(matrix[top][col])
top += 1
# Top to bottom along right column
for row in range(top, bottom + 1):
result.append(matrix[row][right])
right -= 1
# Right to left along bottom row (guard against single row)
if top <= bottom:
for col in range(right, left - 1, -1):
result.append(matrix[bottom][col])
bottom -= 1
# Bottom to top along left column (guard against single column)
if left <= right:
for row in range(bottom, top - 1, -1):
result.append(matrix[row][left])
left += 1
return result
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int top = 0, bottom = matrix.length - 1;
int left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (int col = left; col <= right; col++)
result.add(matrix[top][col]);
top++;
for (int row = top; row <= bottom; row++)
result.add(matrix[row][right]);
right--;
if (top <= bottom) {
for (int col = right; col >= left; col--)
result.add(matrix[bottom][col]);
bottom--;
}
if (left <= right) {
for (int row = bottom; row >= top; row--)
result.add(matrix[row][left]);
left++;
}
}
return result;
}
}
var spiralOrder = function(matrix) {
const result = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let col = left; col <= right; col++)
result.push(matrix[top][col]);
top++;
for (let row = top; row <= bottom; row++)
result.push(matrix[row][right]);
right--;
if (top <= bottom) {
for (let col = right; col >= left; col--)
result.push(matrix[bottom][col]);
bottom--;
}
if (left <= right) {
for (let row = bottom; row >= top; row--)
result.push(matrix[row][left]);
left++;
}
}
return result;
};
| Problem | Technique |
|---|---|
| LC #54 Spiral Matrix (read) | Boundary shrinking or direction vector |
| LC #59 Spiral Matrix II (generate) | Same boundaries, fill values 1..n² instead of reading |
| LC #48 Rotate Image | Transpose + reverse rows in-place |
| LC #73 Set Matrix Zeroes | Use first row/col as markers |
| LC #289 Game of Life | Encode two states in one cell |
Tag on DSAPrep.dev under Matrix and Simulation.