Rotate a Square Matrix 90 Degrees Clockwise

Quick Overview

A coding exercise that rotates an n by n integer matrix 90 degrees clockwise and returns the result. It tests the index mapping between rows and columns, small edge cases such as a one-by-one matrix, and the ability to perform the rotation in place with constant extra memory.

Rotate a Square Matrix 90 Degrees Clockwise

Company: Microsoft

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

Given an `n × n` matrix of integers, rotate it 90 degrees clockwise. After the rotation, the first row of the result is the original first column read from bottom to top, the second row is the original second column read from bottom to top, and so on. The report describes this second problem of the assessment only as a medium problem on rotating a matrix 90 degrees clockwise; it is rebuilt here as the standard square-matrix version. The usual expectation is to rotate the matrix in place, using only a constant amount of memory beyond the matrix itself, so be ready to meet that requirement. The console checks only the matrix you return. ### Function Signature ```python def rotate_clockwise(matrix: list[list[int]]) -> list[list[int]]: ``` ### Rules - `matrix` has `n` rows, each with `n` integers. - Return the rotated matrix: the element at row `i`, column `j` of the input ends up at row `j`, column `n - 1 - i` of the result, with rows and columns numbered from 0. - You may modify `matrix` and return it; the in-place version with constant extra memory is the intended one. ### Constraints - `1 <= n <= 100` - `-1000 <= matrix[i][j] <= 1000` ### Examples **Example 1** ```text Input: matrix = [[1, 2], [3, 4]] Output: [[3, 1], [4, 2]] ``` The first column, read from bottom to top, is `3, 1`; the second column is `4, 2`. **Example 2** ```text Input: matrix = [[2, -1, 0], [5, 7, 3], [-4, 8, 6]] Output: [[-4, 5, 2], [8, 7, -1], [6, 3, 0]] ``` The columns of the input, each read from bottom to top, are `-4, 5, 2`, then `8, 7, -1`, then `6, 3, 0`. The center element `7` stays in place. **Example 3** ```text Input: matrix = [[5]] Output: [[5]] ``` A one-by-one matrix is unchanged.

Overview: A coding exercise that rotates an n by n integer matrix 90 degrees clockwise and returns the result. It tests the index mapping between rows and columns, small edge cases such as a one-by-one matrix, and the ability to perform the rotation in place with constant extra memory.

Microsoft logo
Microsoft
Aug 28, 2026
mediumData ScientistOnline AssessmentCoding & Algorithms
0
0

Given an n × n matrix of integers, rotate it 90 degrees clockwise. After the rotation, the first row of the result is the original first column read from bottom to top, the second row is the original second column read from bottom to top, and so on.

The report describes this second problem of the assessment only as a medium problem on rotating a matrix 90 degrees clockwise; it is rebuilt here as the standard square-matrix version. The usual expectation is to rotate the matrix in place, using only a constant amount of memory beyond the matrix itself, so be ready to meet that requirement. The console checks only the matrix you return.

Function Signature

def rotate_clockwise(matrix: list[list[int]]) -> list[list[int]]:

Rules

  • matrix has n rows, each with n integers.
  • Return the rotated matrix: the element at row i , column j of the input ends up at row j , column n - 1 - i of the result, with rows and columns numbered from 0.
  • You may modify matrix and return it; the in-place version with constant extra memory is the intended one.

Constraints

  • 1 <= n <= 100
  • -1000 <= matrix[i][j] <= 1000

Examples

Example 1

Input:  matrix = [[1, 2], [3, 4]]
Output: [[3, 1], [4, 2]]

The first column, read from bottom to top, is 3, 1; the second column is 4, 2.

Example 2

Input:  matrix = [[2, -1, 0], [5, 7, 3], [-4, 8, 6]]
Output: [[-4, 5, 2], [8, 7, -1], [6, 3, 0]]

The columns of the input, each read from bottom to top, are -4, 5, 2, then 8, 7, -1, then 6, 3, 0. The center element 7 stays in place.

Example 3

Input:  matrix = [[5]]
Output: [[5]]

A one-by-one matrix is unchanged.