Reported April 2022
ZipRecruitermatrix

Rotate a Square Matrix 90 Degrees Clockwise

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

The ZipRecruiter OA reported in April 2022 hands you a plain 2D array and asks for a clockwise quarter-turn. The whole solution hinges on the matrix itself, indexed in place, with no queue or graph anywhere near it. It's the classic rotate-image problem, and the catch is the in-place rule: no second n x n grid in the final approach. If you've seen it, it's five minutes. If you blank on the index math, it's a spiral of off-by-one bugs. StealthCoder sits invisibly on your screen during the live OA as a safety net if the trick slips away.

The problem

You are given an n × n integer matrix representing an image. Rotate the image by 90 degrees clockwise.
Perform the rotation in place by modifying matrix directly. Return that same matrix after the mutation so the runner can compare its final values; do not allocate another n × n matrix in the final approach.

Function
rotateImage(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 former bottom-left element becomes the new top-left element, and every position moves through one clockwise quarter-turn.
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]]
Each original column becomes a row in reverse vertical order.

Constraints
n == matrix.length == matrix[i].length.
1 <= n <= 20.
-1000 <= matrix[i][j] <= 1000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Skip the clever four-way cycle swap unless you remember it cold. The clean trick is two passes: transpose the matrix (swap matrix[i][j] with matrix[j][i] for j > i), then reverse each row. That's a 90 degree clockwise rotation, in place, O(n^2) time and O(1) extra space. Check it on Example 1: transpose gives [[1,4,7],[2,5,8],[3,6,9]], reverse rows gives [[7,4,1],[8,5,2],[9,6,3]]. Pitfalls: looping j from 0 instead of i+1 swaps everything twice and undoes the transpose. Also, don't confuse clockwise with counterclockwise, where you reverse columns or reverse rows first. Return the same matrix object. With n up to 20, performance is irrelevant, so correctness wins. If you freeze on the OA, StealthCoder can hand you this transpose-and-reverse solution so you can check it against your own work.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Rotate a Square Matrix 90 Degrees 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as rotate image. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Rotate a Square Matrix 90 Degrees Clockwise FAQ

How hard is the ZipRecruiter rotate matrix question really?+

It's a medium on paper but easy once you know transpose plus reverse. The risk is index mistakes under pressure, not the concept. With n capped at 20, there are no performance traps. Test on the 3x3 example before submitting and you're likely fine.

What's the trick to rotating in place?+

Transpose the matrix by swapping across the diagonal, then reverse every row. That gives a clockwise 90 degree turn with no extra grid. For counterclockwise, reverse each row first and then transpose, or reverse columns after transposing.

Why is BFS listed as a hint when this looks like a matrix problem?+

Ignore it. Nothing here needs traversal order, a queue, or neighbors. It's pure index manipulation on an array of arrays. Treat it as a matrix problem and don't overthink the label.

Can I just build a new matrix and copy it back?+

The statement says not to allocate another n x n matrix in the final approach. A copy might pass the value check, but it breaks the stated rule. Use transpose and reverse so you meet the constraint and don't risk a rejection.

How do I prepare in 48 hours for this one?+

Write transpose plus row reverse from memory three times, then trace both examples by hand. Also know the layer-by-layer four-cell swap as a backup. Watch the inner loop bound j = i + 1, since that's where most bugs come from.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it