Rotate a Square Thumbnail Pixel Grid 90 Degrees In Place
Last updated: November 29, 2026
Quick Overview
Rotate an n x n grid of pixel values 90 degrees clockwise in place, as when correcting a thumbnail's orientation, using O(1) extra memory.
Meta
November 29, 20260
0
0 solved
Rotate an n x n grid of pixel values 90 degrees clockwise in place, as when correcting a thumbnail's orientation, using O(1) extra memory.
A Meta technical screen question on in-place matrix manipulation. The interviewer wants either the transpose-then-reverse decomposition or the four-way cycle swap, with a clear argument for why the index mapping is right.
What the Interviewer Expects
- State the index mapping (r, c) -> (c, n - 1 - r)
- Decompose into transpose plus row reverse
- Or rotate layer by layer with four-way swaps
- Do it in place with O(1) extra space
- Discuss the counterclockwise and 180 degree variants
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Rotate counterclockwise instead.
- How do you rotate a non-square m x n image?
- How would you rotate a huge image stored in tiles on disk?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: n x n list of lists. Output: none; the grid is modified so the original row 0 becomes the last column. No second n x n buffer allowed....
High-Level Design
Clockwise rotation sends cell (r, c) to (c, n - 1 - r). That mapping is the composition of two simple in-place operations:
- Transpose: swap (r, c) with (c, r) for c > r. Now (r, c) holds what was a...