The Rotate Image interview question asks you to rotate an n×n 2D matrix 90 degrees clockwise in-place. You cannot allocate another 2D matrix — the rotation must be performed directly on the input matrix. This is a foundational matrix manipulation problem with a clean mathematical solution using two operations: transpose and horizontal flip.
This problem is asked at J.P. Morgan, Apple, Cisco, Uber, Goldman Sachs, Microsoft, Meta, Amazon, Google, Bloomberg, Adobe, and virtually every major tech company because it appears in image processing (rotating photos), game development (rotating game boards), and computer graphics. The in-place constraint forces candidates to think mathematically rather than using a simple copy — separating those who understand matrix geometry from those who rely on extra space.
The pattern is transpose then horizontal reverse (for 90° clockwise rotation). Transpose: swap matrix[i][j] with matrix[j][i] for all i < j. Horizontal reverse: reverse each row. For 90° counter-clockwise: transpose then vertical reverse (reverse each column). Both operations are O(n^2) time and O(1) space.
Alternatively, use the four-cell rotation: rotate four corresponding cells simultaneously, one layer at a time from outside in.
Matrix:
1 2 3
4 5 6
7 8 9
Step 1 — Transpose (swap across main diagonal):
1 4 7
2 5 8
3 6 9
Step 2 — Reverse each row:
7 4 1
8 5 2
9 6 3
Result: 90° clockwise rotation. Verify: original corner 1 (top-left) → bottom-left after 90° CW. ✓
(i,j) with (j,i) when i < j to avoid double-swapping.For the Rotate Image coding problem, the array and matrix interview pattern via transpose-then-flip is the clean solution. Memorize: 90° CW = transpose + reverse each row; 90° CCW = transpose + reverse each column; 180° = reverse each row + reverse each column. Interviewers at Tesla and Nvidia often ask for all three rotation directions — knowing these three rules impresses. Practice on a 3×3 matrix to verify your transpose boundaries.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Magic Squares In Grid | Medium | Solve | |
| Number of Laser Beams in a Bank | Medium | Solve | |
| Minimum Operations to Make a Uni-Value Grid | Medium | Solve | |
| Number Of Corner Rectangles | Medium | Solve | |
| Remove All Ones With Row and Column Flips | Medium | Solve |