March 24
Rotate Image
How to decompose a matrix rotation into simpler transformations and do it in place with clear coordinate mapping.
Matrix Intermediate 20 min TypeScript
Given an n x n matrix, rotate the image by 90 degrees clockwise.
In the original problem, the rotation must be done in place.
In the SeniorPath playground, apply the transformation in place and then return the matrix itself for comparison.
Example:
Input:
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
Output:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
What to notice before coding
- The matrix is square. That simplifies the transformation a lot.
- The challenge is not only "swap elements." It is mapping coordinates without getting lost.
- There is an elegant decomposition into two simpler transformations.
Common mistakes
- trying to do four-position swaps manually without a clear plan
- forgetting that the original problem asks for in-place work
Step 1 - Understand the most direct baseline
The most obvious way to rotate is:
- create a new matrix
- for each value, calculate its new coordinate
It works.
But it uses extra memory and avoids the real request.
Step 2 - Look for a simpler decomposition
Instead of treating rotation as one magical step, break it into:
- transpose the matrix
- reverse each row
Example:
[1 2 3] [1 4 7] [7 4 1]
[4 5 6] -> [2 5 8] -> [8 5 2]
[7 8 9] [3 6 9] [9 6 3]
Step 3 - Understand transposition
To transpose means:
- swap
matrix[row][col]withmatrix[col][row]
To avoid undoing swaps, you only need to walk above the main diagonal.
Step 4 - Reverse each row
After transposition, each row only needs to be reversed with two pointers.
The final result is the 90-degree clockwise rotation.
The interactive editor needs JavaScript. You can still read the prompt and copy the starter code below.
Starter code
export function rotate(matrix: number[][]): number[][] {
// your solution here
return matrix
}
Haven't solved it yet?
Viewing the solution now may reduce learning. It's worth trying a bit more.
Open the reference solution
Without JavaScript, the reference solution is shown inline instead of in a dialog.
Solution
Final complexity
Time: O(n^2)
Space: O(1)
Solution 1 - Create a new matrix O(n²)
Good baseline because the coordinate mapping stays explicit.
export function rotate(matrix: number[][]): number[][] {
const size = matrix.length
const rotated = Array.from({ length: size }, () => new Array(size).fill(0))
for (let row = 0; row < size; row += 1) {
for (let col = 0; col < size; col += 1) {
rotated[col][size - 1 - row] = matrix[row][col]
}
}
return rotated
}
It works, but it uses extra memory.
Solution 2 - In-place with transpose and reverse using two pointers
Now the same transformation happens inside the matrix itself.
export function rotate(matrix: number[][]): number[][] {
const size = matrix.length
for (let row = 0; row < size; row += 1) {
for (let col = row + 1; col < size; col += 1) {
const temp = matrix[row][col]
matrix[row][col] = matrix[col][row]
matrix[col][row] = temp
}
}
for (const row of matrix) {
let left = 0
let right = row.length - 1
while (left < right) {
const temp = row[left]
row[left] = row[right]
row[right] = temp
left += 1
right -= 1
}
}
return matrix
}
The strong insight here is decomposing one hard operation into two simple and reliable ones.
What to say in the interview
A strong short explanation would be:
The baseline creates another matrix and moves each value to its rotated coordinate. Since the original requires in-place work, the stronger version decomposes the rotation into transpose plus reverse each row. That keeps
O(n²)time while removing the extraO(1)space cost.
That shows that you:
- did not get stuck on manual four-position swaps
- knew how to decompose the problem
- justified the in-place adaptation
When this pattern shows up again
This pattern comes back when you need to:
- map matrix coordinates
- do in-place transformations
- break a complex operation into simpler steps
The point is not memorizing
Rotate Image.
The point is learning how to decompose matrix transformations clearly.