Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Rotate Image
Problem
You are given a square grid of integers representing an image, n rows by n columns. Rotate the image 90 degrees clockwise in place: modify the given 2D list directly and do not allocate a second n x n grid.
- The grid is at most about 25 rows/columns on a side.
- Cell values are small integers, positive or negative.
Examples
[[2,9,4],[7,5,3],[6,1,8]]→[[6,7,2],[1,5,9],[8,3,4]]. The left column6,7,2(read bottom to top) becomes the top row.[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]→[[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]].[[1]]→[[1]]; a 1x1 image is unchanged.
Hints
- Where does the element at
(r, c)end up after a clockwise quarter turn? Write the formula. - That mapping can be decomposed into two simpler operations, each of which is easy to do in place.
- Transpose the matrix (swap
(r, c)with(c, r)) and then reverse each row. Alternatively, rotate four elements at a time in cycles.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.