Rotate a Square Matrix Clockwise
Updated
Problem statement
Given an n x n integer matrix, rotate it exactly 90 degrees clockwise in place.
Do not allocate another n x n matrix. Return the same matrix after the rotation so the result can be checked.
Function
rotateMatrixClockwise(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]]Moved or new
The left column becomes the top row, and every other value moves to its clockwise position.
Example 2
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]return = [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]Moved or new
Both square layers rotate clockwise.
Example 3
matrix = [[5]]return = [[5]]A one-cell matrix is unchanged.
Constraints
1 <= n <= 200.matrix.length == nand every row has lengthn.-10^9 <= matrix[row][col] <= 10^9.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Tic-Tac-Toe Move WinnersByteDance · ONSITE INTERVIEWByteDance interview questions
- Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
- Cyclic Fuel Command SimulationWaymo · ONSITE INTERVIEWWaymo interview questions
- Generate a Spiral MatrixMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
- Spiral Matrix with ObstaclesTemu · ONSITE INTERVIEWTemu interview questions
- Stateful Neuron Matrix UpdateScale AI · ONSITE INTERVIEWScale AI interview questions