Rotate a Matrix 90 Degrees Clockwise

Hard ⏱ 18 min 35% acceptance ★★★★★ 4.5
Write rotate_matrix_clockwise(matrix) that returns a new matrix representing matrix rotated 90 degrees clockwise. Assume matrix is square (n x n).

Examples

Example 1
Input
matrix = [[1, 2], [3, 4]]
Output
[[3, 1], [4, 2]]
Explanation

Transposing then reversing each row (or reversing rows then transposing) rotates clockwise.

Constraints

  • 1 <= n <= 200
  • matrix is square.

Topics

ListsMatrixNested Lists

Companies

MicrosoftAmazon

Hints

Hint 1

Rotating 90 clockwise = transpose the matrix, then reverse each row.

Hint 2

Alternatively: take columns from bottom to top as new rows.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.