Rotate a Square Warehouse Floor Map 90 Degrees In Place
Last updated: November 29, 2026
Quick Overview
A square floor map of an Amazon fulfillment center is stored as an n x n matrix. Rotate it 90 degrees clockwise in place, without allocating a second matrix.
Amazon
November 29, 20260
0
0 solved
A square floor map of an Amazon fulfillment center is stored as an n x n matrix. Rotate it 90 degrees clockwise in place, without allocating a second matrix.
Robotics teams re-orient floor maps when a site layout is ingested with a different compass reference. The interviewer is checking index reasoning and whether you know the transpose-then-reverse trick or the four-way cycle swap.
What the Interviewer Expects
- Map the rotation: element (i, j) moves to (j, n - 1 - i)
- Do it in place with transpose plus reversing each row
- Or explain the layer-by-layer four-element cycle
- State O(n^2) time and O(1) extra space
- Verify on a 2 x 2 and a 3 x 3 example
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
- How would you rotate counter-clockwise?
- How would you rotate a non-square m x n map, and why can that not be in place easily?
- How would you rotate by 180 degrees in one pass?
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: an n x n list of lists. Output: none; the same matrix must hold the clockwise rotation. n can be 0 or 1. Extra memory must be O(1), so building a new matrix is not acceptable....
High-Level Design
A clockwise rotation equals a transpose (swap (i, j) with (j, i)) followed by reversing every row. After transposing, (i, j) holds the original (j, i); reversing row i moves column j to column n - 1 -...