Back to Practice
#0250
Rotate Image Matrix
MediumDSA28 min20 XP
Problem
Given an n x n matrix, return a new matrix rotated 90 degrees clockwise.
Why This Matters
Matrix rotation combines coordinate thinking with a practical transform used in image and grid problems.
Function Signature
def rotate_matrix(matrix):
Examples
Example 1
Inputmatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
The first row becomes the last column.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Three by three
Input[[1,2,3],[4,5,6],[7,8,9]]
Expected[[7,4,1],[8,5,2],[9,6,3]]
Every cell moves from (r, c) to (c, n - 1 - r).
One by one
Input[[5]]
Expected[[5]]
A single cell stays where it is.
Hidden Test Categories
two by two matrixnegative valueslarger square matrix