Rotate a Square Matrix 90 Degrees
Updated
Problem statement
You are given an n x n integer matrix matrix. Rotate it by 90 degrees clockwise and return the result.
After the rotation, the value that was in row r and column c is in row c and column n - 1 - r. In other words, the first column, read from bottom to top, becomes the first row.
Try to rotate the matrix in place, using only a constant amount of extra memory, and then return matrix itself. The judge checks only the matrix you return, so returning a new rotated matrix is also accepted.
Function
rotateMatrix(matrix: int[][]) → int[][]Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]return = [[7,4,1],[8,5,2],[9,6,3]]The first column read from bottom to top, 7, 4, 1, becomes the first row. The middle column read from bottom to top, 8, 5, 2, becomes the second row, and the last column, 9, 6, 3, becomes the third row. The center value 5 stays where it is.
Example 2
matrix = [[-3,10],[0,7]]return = [[0,-3],[7,10]]Each corner moves one corner clockwise: top-left -3 goes to top-right, top-right 10 goes to bottom-right, bottom-right 7 goes to bottom-left, and bottom-left 0 goes to top-left.
Example 3
matrix = [[42]]return = [[42]]A 1 x 1 matrix looks the same after any rotation.
Constraints
1 <= n <= 50matrix.length == matrix[i].length == n-1000 <= matrix[i][j] <= 1000
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions