FastPrepRotate a Square Matrix Clockwise
Problems/Rotate a Square Matrix Clockwise

Rotate a Square Matrix Clockwise

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report describes rotating a matrix layer by layer, but leaves out the angle, direction, shape, and function signature. Our version is a square matrix rotated in place by 90 degrees clockwise. Our version is about 78% the same as the reported question.

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]]
matrix
012012314562789
→
return
012074118522963
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]]
matrix
0123051911124810213367315141216
→
return
0123015132511434121268931671011
Moved or new

Both square layers rotate clockwise.

Example 3

matrix = [[5]]return = [[5]]
matrix
005
→
return
005

A one-cell matrix is unchanged.

Constraints

  • 1 <= n <= 200.
  • matrix.length == n and every row has length n.
  • -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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[][] rotateMatrixClockwise(int[][] matrix) {
      // Write your code here.
    }
}
Checking account…