Rotate a Matrix by 90 Degrees
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Mygate reportedly put a matrix rotation problem in front of candidates in September 2023, and the input size is the first thing to read. With n capped at 40, brute force isn't a runtime problem. The real ask is doing it in place with O(1) extra space. The hinted tag says breadth-first-search, but this is a plain matrix problem, so ignore that label. If you've got the OA coming up, learn the transpose-then-reverse trick and you're mostly done. If your mind goes blank mid-assessment, StealthCoder runs invisibly on screen as a safety net and hands you the solution in real time.
The problem
Given an n × n integer matrix, rotate it 90 degrees clockwise in place and return the modified matrix. The original value at row r and column c moves to row c and column n - 1 - r. Rows and columns use zero-based indices. Keep the matrix square and preserve every value. An approach using temporary storage is valid; aim to reduce auxiliary space to O(1). Function rotateMatrix(matrix: int[][]) → int[][] Examples Example 1 matrix = [[1,2],[3,4]] return = [[3,1],[4,2]] The original bottom-left value 3 moves to the top-left, and the original top-right value 2 moves to the bottom-right. Example 2 matrix = [[2,5,8],[1,4,7],[0,3,6]] return = [[0,1,2],[3,4,5],[6,7,8]] Each original column becomes a row, read from bottom to top. Constraints 1 <= n <= 40. Every row contains exactly n integers. Every value is in [-10^6, 10^6].
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 gives a 90 degree clockwise rotation, and it matches the mapping in the statement: (r, c) goes to (c, n-1-r). The alternative is rotating four cells at a time, layer by layer, which also gives O(1) space. The common pitfall is swapping across the whole matrix in the transpose step, which swaps every pair twice and undoes your work. Start the inner loop at j = i + 1. Another mistake is allocating a new matrix and calling it done when the prompt wants O(1) auxiliary space. Check your result against Example 1, [[1,2],[3,4]] to [[3,1],[4,2]], before submitting. If you freeze on the index math during the live OA, StealthCoder is the hedge that gets you the working code. Complexity is O(n^2) time and O(1) space.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Rotate a Matrix by 90 Degrees 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 would have shipped this the night before his JPMorgan OA if he'd had it.
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 Mygate's OA.
Mygate reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rotate a Matrix by 90 Degrees FAQ
How hard is the Mygate matrix rotation question really?+
It's easy to medium. The logic is short, but the index math trips people up under pressure. With n at most 40, performance isn't the issue. Clean in-place swaps are. If you've seen transpose plus reverse once, you can write it in a few minutes.
What's the trick to rotating a matrix 90 degrees clockwise in place?+
Transpose, then reverse each row. Swap matrix[i][j] with matrix[j][i] only for j greater than i, then reverse every row. That satisfies the (r, c) to (c, n-1-r) mapping with no extra matrix and O(1) auxiliary space.
Is BFS needed here since the tag says breadth-first-search?+
No. This is a matrix manipulation problem. There's no graph, no shortest path, no traversal order. Loop over indices and swap values. Treat the BFS hint as noise and don't build a queue.
Can I just build a new matrix to save time?+
The statement says temporary storage is valid, so it'll likely pass. Set newMatrix[c][n-1-r] = matrix[r][c]. But the prompt says aim for O(1) space, so the in-place version is the safer answer if there's any follow-up.
How do I prepare for this in 48 hours?+
Write the transpose-and-reverse solution from memory twice. Then write the four-way layer rotation once. Test on a 2x2 and a 3x3 by hand, using the examples in the problem. Watch the loop bounds, since that's where most bugs come from.