Problem
You get a matrix matrix with m rows and n columns. A matrix is a grid of numbers. Return all the items of the matrix in spiral order.
Spiral order starts at the top-left item and moves right along the top row. Then it moves down the right column, left along the bottom row and up the left column. Then it does the same steps on the remaining inner part, until it visits each item.
Example 1
- Input:
- matrix = [[2,4,6],[8,10,12]]
- Output:
- [2,4,6,12,10,8]
- Explanation:
- The top row gives 2, 4 and 6. The right column gives 12, and the bottom row, from right to left, gives 10 and 8.
Example 2
- Input:
- matrix = [[3,1,4],[1,5,9],[2,6,5]]
- Output:
- [3,1,4,9,5,6,2,1,5]
- Explanation:
- The outer ring gives 3, 1, 4, 9, 5, 6, 2 and 1. The center item 5 comes last.
Example 3
- Input:
- matrix = [[7],[8],[9]]
- Output:
- [7,8,9]
- Explanation:
- The matrix has one column, so the order goes from top to bottom.
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Candy Crush
Coding · Array · Matrix · Simulation
- Contain Virus
Coding · Array · Breadth-First Search · Depth-First Search
- Match Alphanumerical Pattern in Matrix I
Coding · Array · Hash Table · Matrix
- Search a 2D Matrix
Coding · Array · Binary Search · Matrix
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 4 records
Capital One
Leetcode 54. Spiral Matrix
Reported Sep 4, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceLiveramp
Leetcode 54. Spiral Matrix
Reported Sep 9, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 54. Spiral Matrix
Reported Oct 7, 2025 · 1 source report
STAFF
Source snapshot: Nov 12, 2026
View sourceMicrosoft
Leetcode 54. Spiral Matrix
Reported Jan 3, 2026 · 3 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View source