54. Spiral Matrix
Spiral Matrix (LeetCode 54) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Spiral Traversal subpattern.
- Blind 75 · #69 of 75 · Math & Geometry
- NeetCode 150 · #137 of 150 · Math & Geometry
- NeetCode 250 · #233 of 250 · Math & Geometry
How to solve Spiral Matrix
Spiral Matrix is a Spiral Matrix Traversal problem, part of Array/Matrix Manipulation Patterns. Master spiral traversal pattern for 2D matrices. Learn to traverse matrices in spiral order for interview problems.
Work towards the solution in these steps:
- Well for some problems, the best way really is to come up with some algorithms for simulation. Basically, you need to simulate what the problem asks us to do.
- We go boundary by boundary and move inwards. That is the essential operation. First row, last column, last row, first column, and then we move inwards by 1 and repeat. That's all. That is all the simulation that we need.
- Think about when you want to switch the progress on one of the indexes. If you progress on i out of [i, j], you'll shift in the same column. Similarly, by changing values for j, you'd be shifting in the same row. Also, keep track of the end of a boundary so that you can move inwards and then keep repeating. It's always best to simulate edge cases like a single column or a single row to see if anything breaks or not.
The reference solution runs in O(m * n) time and O(1) space.
The Spiral Matrix solution and editorial walks through the full approach with reference code.
Problem statement
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.length
- n == matrix[i].length
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100
Topics and companies
Topics: Array, Matrix, Simulation.
Reported in interviews at Microsoft, Apple, Intuit, Facebook, Google, Amazon, ByteDance, Expedia, Paypal, eBay.
How to practise Spiral Matrix on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m * n) time and O(1) space.
Open Spiral Matrix in the code editor, or read the Spiral Matrix editorial for a worked solution with its approach and complexity analysis.
Where Spiral Matrix sits in the DSA pattern sheet
- Array/Matrix Manipulation Patterns pattern guide
- Spiral Traversal subpattern
- The full DSA patterns sheet
Problems related to Spiral Matrix
Other problems that use the same Spiral Traversal and Array/Matrix Manipulation Patterns techniques:
- Spiral Matrix II — Medium, same subpattern
- Spiral Matrix III — Medium, same subpattern
- Spiral Matrix IV — Medium, same subpattern
- Add Binary — Easy, same pattern
- Add to Array-Form of Integer — Easy, same pattern
- Concatenation of Array — Easy, same pattern
Spiral Matrix FAQ
What is the LeetCode number of Spiral Matrix?
Spiral Matrix is LeetCode problem 54, rated Medium.
What pattern is used to solve Spiral Matrix?
Spiral Matrix is a Spiral Matrix Traversal problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Spiral Traversal subpattern.
What is the time complexity of Spiral Matrix?
The reference solution runs in O(m * n) time and O(1) space.
Which companies ask Spiral Matrix?
Spiral Matrix has been reported in interviews at Microsoft, Apple, Intuit, Facebook, Google, Amazon, ByteDance, Expedia, Paypal, eBay.