Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-059

Rotate Matrix 90 Degrees

Problem

Rotate an n by n matrix 90 degrees clockwise and return the new matrix, leaving the input unchanged.

The element at row i, column j moves to row j, column n - 1 - i. Return an empty list for empty input.

Hint

A clockwise rotation sends the first row to the last column.

Test cases

case 1
in: [[[1,2],[3,4]]]
expected: [[3,1],[4,2]]
case 2
in: [[[1,2,3],[4,5,6],[7,8,9]]]
expected: [[7,4,1],[8,5,2],[9,6,3]]
case 3
in: [[[1]]]
expected: [[1]]
case 4
in: [[]]
expected: []
Starter codePython
def rotate_matrix(matrix):
    # Your code here
    pass

4 test cases · run your code against them in the editor