Reported November 2020
SambaNova Systemsmatrix

Rotate Image

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

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

A 90 degree clockwise rotation sounds like index gymnastics, but it really reduces to two simple moves: transpose the matrix, then reverse each row. SambaNova Systems reported this one in November 2020, and it's the classic in-place Rotate Image problem on an n x n grid. If you've seen it, you'll finish fast. If you haven't, the trick is easy to miss under pressure. Don't allocate a second matrix, because the statement says the final approach can't. StealthCoder is there as a safety net during the live OA if your mind goes blank on the swap order, but this pattern is short enough to own tonight.

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

The trick: a clockwise quarter-turn equals a transpose (swap matrix[i][j] with matrix[j][i] for j > i) followed by reversing every row. That's O(n^2) time and O(1) extra space, which satisfies the in-place rule. The common pitfall is transposing the full grid, which swaps every pair twice and returns the original. Start the inner loop at i+1. The second pitfall is mixing up directions. Reverse rows for clockwise. For counterclockwise you'd reverse columns instead, or reverse rows first then transpose. Check it on Example 1: transpose gives [[1,4,7],[2,5,8],[3,6,9]], reversing rows gives [[7,4,1],[8,5,2],[9,6,3]]. With n up to 20, performance is never the issue. Correctness is. If you blank during the live OA, StealthCoder can hand you the two-step solution, but you should be able to write it from memory.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Rotate Image 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

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 SambaNova Systems's OA.

SambaNova Systems reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Rotate Image FAQ

How hard is Rotate Image really?+

It's medium on paper but easy once you know the transpose plus reverse trick. With n at most 20, there's no performance trap. The only difficulty is getting loop bounds and direction right, which you can verify with the 3x3 example in under a minute.

What's the trick for rotating in place?+

Transpose the matrix by swapping across the main diagonal, then reverse each row. That gives a clockwise 90 degree turn with no extra matrix. Only swap pairs where j is greater than i, or you'll undo your own work.

Do I need to return the matrix or just mutate it?+

Both. The statement says to modify the matrix directly and also return that same matrix so the runner can compare final values. Mutate in place, then return matrix. Don't build and return a new grid.

Is there another way besides transpose and reverse?+

Yes. Rotate four cells at a time layer by layer, moving top to right to bottom to left to top. It's also O(1) space but the index math is easier to get wrong. Transpose and reverse is simpler and safer under time pressure.

How do I prepare for this in 48 hours?+

Write the transpose and reverse version by hand twice, then test it on the 3x3 and 4x4 examples. Also rehearse the counterclockwise variant, since interviewers like to flip the direction. That's about thirty minutes of work and it covers this whole family.

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

OA at SambaNova Systems?
Invisible during screen share
Get it