Rotate a Square Matrix Clockwise
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Google OA reported in April 2026 asks you to rotate an n x n matrix 90 degrees clockwise, in place. It looks like a warm-up. It's not, because the moment you allocate a second matrix you've broken the rule and the checker may fail you. The hinted pattern says breadth-first-search, but this is really a matrix problem with index math. The edge case that breaks a naive solution is the odd-sized grid, where the center cell stays put and your loop bounds must not touch it twice. If you blank on the indices, StealthCoder is the safety net running invisibly during the live OA.
The problem
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]] 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]] Both square layers rotate clockwise. Example 3 matrix = [[5]] return = [[5]] 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two passes. Transpose the matrix by swapping matrix[i][j] with matrix[j][i] for j > i, then reverse each row. That's a clockwise rotation in O(n^2) time and O(1) extra space. The common pitfall is swapping across the full grid in the transpose, which undoes every swap and returns the original. Start j at i+1. The other trap is the layer-by-layer four-way swap, where off-by-one errors on odd n corrupt the center or the ring edges. Test the 1x1 case and a 3x3 by hand before submitting. Reversing rows versus columns also decides clockwise versus counterclockwise, so check Example 1 against your output. BFS has no role here. If the index math slips under pressure, StealthCoder can hand you the clean transpose-and-reverse version while the OA is live.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Rotate a Square Matrix Clockwise cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as rotate image. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rotate a Square Matrix Clockwise FAQ
What's the trick to rotating a matrix in place?+
Transpose, then reverse each row. Transpose swaps matrix[i][j] with matrix[j][i] only for j greater than i. Reversing each row afterward gives a 90 degree clockwise turn. It runs in O(n^2) time with O(1) extra space, which satisfies the no second matrix rule.
Is this really a BFS problem?+
No. The hint says breadth-first-search, but nothing here involves traversal, queues, or graphs. It's pure matrix index manipulation. Don't waste time hunting for a search approach. Focus on transpose plus reverse, or the four-way layer swap.
What edge cases break a naive solution?+
The 1x1 matrix, odd n where the center cell must stay untouched, and a transpose that loops over the whole grid and swaps every pair twice. Also confirm you reverse rows, not columns, since that flips clockwise into counterclockwise.
How is the Google April 2026 version checked?+
The statement says to return the same matrix after rotation so the result can be checked. Mutate the input and return it. Allocating a new n x n matrix violates the constraint, even if the output values look correct.
How do I prepare for this in 48 hours?+
Write the transpose and reverse solution from memory twice, then trace Example 1 and the 4x4 Example 2 by hand. Then code the layer-swap version once as a backup. With n up to 200, performance isn't a concern, correctness of indices is.