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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
Matrix manipulation
In-place algorithms
Transpose
Index mapping
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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:

  1. Transpose: swap (r, c) with (c, r) for c > r. Now (r, c) holds what was a...

Submit Your Answer
Markdown supported