Spiral Traversal of Matrix
Boundary PointersSolved Nov 8, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a 2D square matrix, print all its elements in a spiral (clockwise) order. The problem specifically asks for a solution without using extra space (e.g., using a visited array).ReadHide
Problem Description
Given a 2D square matrix, print all its elements in a spiral (clockwise) order. The problem specifically asks for a solution without using extra space (e.g., using a visited array).
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use four pointers (top, bottom, left, right) to define boundaries. Iterate through the four sides, shifting the boundaries inward after each pass.
Approaches
1. Boundary Pointers (Optimal) - O(N²) ⭐
Idea: The complexity is fixed at O(N²) as every element must be visited. The optimal method uses four pointers to track the current processing boundaries: top, bottom, left, and right.
The algorithm loops while the boundaries haven't crossed (top <= bottom and left <= right), executing four steps: traverse right (increment top), traverse down (decrement right), traverse left (decrement bottom), traverse up (increment left).
Time: O(N²)
Space: O(1) (Excluding output storage).
Solution code
import sys
def spiralTraversal_optimal(matrix):
"""O(N^2) Time, O(1) Space - Boundary Pointers"""
if not matrix or not matrix[0]: return []
R = len(matrix)
C = len(matrix[0])
top, bottom = 0, R - 1
left, right = 0, C - 1
result = []
while top <= bottom and left <= right:
# 1. Traverse Right (Top row)
for j in range(left, right + 1):
result.append(str(matrix[top][j]))
top += 1
# 2. Traverse Down (Right column)
for i in range(top, bottom + 1):
result.append(str(matrix[i][right]))
right -= 1
# 3. Traverse Left (Bottom row) - Check boundary
if top <= bottom:
for j in range(right, left - 1, -1):
result.append(str(matrix[bottom][j]))
bottom -= 1
# 4. Traverse Up (Left column) - Check boundary
if left <= right:
for i in range(bottom, top - 1, -1):
result.append(str(matrix[i][left]))
left += 1
# Print the space-separated output
print(" ".join(result))Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language