Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

54. Spiral Matrix

Medium52.0% Acceptance
ArrayMatrixSimulation
Asked by:
M
Microsoft
ProblemHints (3)Solutions (7)VideosCompanies (22)Notes

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
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
A
Apple
F
Facebook
G
Google
+17

Approach

The Spiral Matrix problem asks you to return all elements of a 2D matrix in spiral order. The key idea is to simulate the traversal layer by layer while maintaining four boundaries: top, bottom, left, and right. These boundaries represent the current outer layer of the matrix that has not yet been visited.

At each step, traverse in four directions: move left-to-right across the top row, top-to-bottom along the right column, right-to-left across the bottom row, and bottom-to-top along the left column. After completing each direction, update the corresponding boundary to shrink the traversal region. Continue this process until the boundaries cross.

This approach works well because it avoids revisiting elements and cleanly models the spiral pattern using simple index updates. The traversal touches each element exactly once, making it efficient for large matrices. Care must be taken to check boundary conditions after each step to prevent duplicate traversals in cases where the matrix has uneven dimensions.

Complexity

ApproachTime ComplexitySpace Complexity
Boundary Simulation (Spiral Traversal)O(m × n)O(1) extra space (excluding output)

Video Solution Available

take U forward

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

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.

2
Hint 2

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.

3
Hint 3

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.

Ready to see the solutions?View Solutions

Solutions (7)

Iterative Boundary Management

This approach involves simulating the spiral order by iteratively traversing matrix layers. The boundaries are adjusted after each complete traversal of a side (top, right, bottom, left). By shrinking the boundaries, we can eventually cover the entire matrix in a spiral order.

Time Complexity: O(m * n) where m is the number of rows and n is the number of columns, since each element is accessed once.

Space Complexity: O(1) additional space, excluding the space for the output array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int

Explanation

The C solution uses a while loop to traverse the matrix. It defines boundaries for the layers (top, bottom, left, right) and adjusts these boundaries after completing the traversal of each side. The movement is controlled by the boundary variables, ensuring the entire matrix is spirally covered.

Recursive Divide and Conquer

This approach uses recursion to divide the matrix into smaller sections. By processing the outer layer of the matrix, we can recursively call the function on the remaining submatrix. This continues until the entire matrix is covered.

Time Complexity: O(m * n) since each recursive call processes an entire layer of the matrix.

Space Complexity: O(m * n) due to the recursive call stack usage, although tail recursion may optimize this depending on the Python implementation.

1def spiralOrder(matrix):
2    result = []
3    if

Video Solutions

Watch expert explanations and walkthroughs

Spiral Traversal of a Matrix | Spiral Matrix

take U forward
16:33308,263 views

Asked By Companies

22 companies
M
Microsoft
A
Amazon
A
Apple
F
Facebook
G
Google
A
Adobe
L
LiveRamp
Z
Zillow
O
Oracle
W
Wipro
j
jio
A
Accenture
P
PayTM
o
oyo
C
Cisco
Z
Zoho
U
Uber
J
JPMorgan
S
SAP Labs
C
Cognizant
S
Salesforce
B
Browserstack

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Valid SudokuMedium
Sudoku SolverHard
Rotate ImageMedium
Spiral Matrix IIMedium
More similar problems

Related Topics

ArrayMatrixSimulation

Problem Stats

Acceptance Rate52.0%
DifficultyMedium
Companies22

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Spiral Matrix asked in FAANG interviews?

Yes, Spiral Matrix is a common medium-level interview problem asked in companies like Amazon, Google, and Microsoft. It tests matrix traversal skills, boundary handling, and simulation logic.

What is the optimal approach for Spiral Matrix?

The optimal approach is boundary-based simulation. Maintain four pointers representing the current top, bottom, left, and right boundaries and traverse the matrix in four directions while shrinking the boundaries after each pass.

What are common mistakes when solving Spiral Matrix?

A frequent mistake is not checking boundary conditions after updating pointers, which can cause duplicate traversal of rows or columns. Handling matrices with a single row or column also requires careful checks.

What data structure is best for solving Spiral Matrix?

The problem mainly uses arrays since the input is a 2D matrix. No additional complex data structures are required—just index variables to track boundaries and a list to store the traversal order.

Previous Problem

Maximum Subarray

Next Problem

Jump Game

*
returnSize
)
{
5
int
top
=
0
,
bottom
=
matrixSize
-
1
,
left
=
0
,
right
=
matrixColSize
[
0
]
-
1
;
6
int
*
result
=
(
int
*
)
malloc
(
(
matrixSize
*
matrixColSize
[
0
]
)
*
sizeof
(
int
)
)
;
7
int
k
=
0
;
8
while
(
top
<=
bottom
&&
left
<=
right
)
{
9
for
(
int
i
=
left
;
i
<=
right
;
++
i
)
result
[
k
++
]
=
matrix
[
top
]
[
i
]
;
10
top
++
;
11
for
(
int
i
=
top
;
i
<=
bottom
;
++
i
)
result
[
k
++
]
=
matrix
[
i
]
[
right
]
;
12
right
--
;
13
if
(
top
<=
bottom
)
{
14
for
(
int
i
=
right
;
i
>=
left
;
--
i
)
result
[
k
++
]
=
matrix
[
bottom
]
[
i
]
;
15
bottom
--
;
16
}
17
if
(
left
<=
right
)
{
18
for
(
int
i
=
bottom
;
i
>=
top
;
--
i
)
result
[
k
++
]
=
matrix
[
i
]
[
left
]
;
19
left
++
;
20
}
21
}
22
*
returnSize
=
k
;
23
return
result
;
24
}
matrix
:
4
def
spiral_layer
(
top
,
bottom
,
left
,
right
)
:
5
if
top
>
bottom
or
left
>
right
:
6
return
7
result
.
extend
(
matrix
[
top
]
[
i
]
for
i
in
range
(
left
,
right
+
1
)
)
8
result
.
extend
(
matrix
[
i
]
[
right
]
for
i
in
range
(
top
+
1
,
bottom
+
1
)
)
9
if
top
<
bottom
:
10
result
.
extend
(
matrix
[
bottom
]
[
i
]
for
i
in
range
(
right
-
1
,
left
-
1
,
-
1
)
)
11
if
left
<
right
:
12
result
.
extend
(
matrix
[
i
]
[
left
]
for
i
in
range
(
bottom
-
1
,
top
,
-
1
)
)
13
spiral_layer
(
top
+
1
,
bottom
-
1
,
left
+
1
,
right
-
1
)
14
spiral_layer
(
0
,
len
(
matrix
)
-
1
,
0
,
len
(
matrix
[
0
]
)
-
1
)
15
return
result

Explanation

The Python solution defines a recursive helper function `spiral_layer`, with parameters defining the boundaries of the current submatrix. The base condition checks if the boundary indices are valid. It extracts and appends elements to the result, then recursively narrows the boundaries until they converge.