Explain in-place matrix rotation.
Skip to solutionKEEP THE
mediumDSA
How do you rotate an N×N matrix by 90 degrees in place?
708 views
01
Understand the problem
matrixin-place
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Transpose the matrix (swap m[i][j] with m[j][i]), then reverse each row. That combination gives a clockwise 90° rotation in place with O(1) extra space and O(n²) time. Reversing columns instead rotates counter-clockwise.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Transpose then reverse rows
Run Playgrounddef rotate(matrix):
n = len(matrix)
for i in range(n): # transpose (upper triangle)
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
for row in matrix: # reverse each row
row.reverse()
return matrix
# --- demo ---
print(rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
# [[7, 4, 1], [8, 5, 2], [9, 6, 3]]05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 55 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.