Rotate a Matrix In Place
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in in-place matrix and array manipulation, understanding of spatial transformations, and consideration of space and time complexity constraints.
Part 1: Rotate Matrix 90 Degrees Clockwise In Place
Constraints
- 0 <= n <= 200
- matrix is square: matrix has n rows and each row has n columns
- -10^9 <= matrix[i][j] <= 10^9
Examples
Input: []
Expected Output: []
Explanation: Empty matrix stays empty.
Input: [[42]]
Expected Output: [[42]]
Explanation: A 1x1 matrix is unchanged after rotation.
Input: [[1,2],[3,4]]
Expected Output: [[3,1],[4,2]]
Explanation: The top row becomes the right column.
Input: [[1,2,3],[4,5,6],[7,8,9]]
Expected Output: [[7,4,1],[8,5,2],[9,6,3]]
Explanation: Standard 3x3 clockwise rotation.
Input: [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Expected Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Explanation: Works for larger square matrices too.
Hints
- Try breaking the rotation into two simpler in-place operations.
- After transposing a matrix, what row operation turns it into a clockwise rotation?
Part 2: Rotate Matrix 90 Degrees Counterclockwise In Place
Constraints
- 0 <= n <= 200
- matrix is square: matrix has n rows and each row has n columns
- -10^9 <= matrix[i][j] <= 10^9
Examples
Input: []
Expected Output: []
Explanation: Empty matrix stays empty.
Input: [[5]]
Expected Output: [[5]]
Explanation: A 1x1 matrix is unchanged after rotation.
Input: [[1,2],[3,4]]
Expected Output: [[2,4],[1,3]]
Explanation: Counterclockwise rotation moves the top row into the left column.
Input: [[1,2,3],[4,5,6],[7,8,9]]
Expected Output: [[3,6,9],[2,5,8],[1,4,7]]
Explanation: Standard 3x3 counterclockwise rotation.
Input: [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Expected Output: [[11,10,7,16],[9,8,6,12],[1,4,3,14],[5,2,13,15]]
Explanation: This demonstrates the in-place transformation on a 4x4 matrix.
Hints
- You can still use an in-place transpose as the first step.
- After transposing, decide whether reversing each row or reversing the order of the rows gives a left turn.
Part 3: Rotate Matrix 180 Degrees In Place
Constraints
- 0 <= n <= 200
- matrix is square: matrix has n rows and each row has n columns
- -10^9 <= matrix[i][j] <= 10^9
Examples
Input: []
Expected Output: []
Explanation: Empty matrix stays empty.
Input: [[9]]
Expected Output: [[9]]
Explanation: A 1x1 matrix is unchanged after rotation.
Input: [[1,2],[3,4]]
Expected Output: [[4,3],[2,1]]
Explanation: Every element moves to its opposite position.
Input: [[1,2,3],[4,5,6],[7,8,9]]
Expected Output: [[9,8,7],[6,5,4],[3,2,1]]
Explanation: Standard 3x3 180-degree rotation.
Input: [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Expected Output: [[16,12,14,15],[7,6,3,13],[10,8,4,2],[11,9,1,5]]
Explanation: Works for larger square matrices as well.
Hints
- A 180-degree rotation maps every element to the position symmetric through the center of the matrix.
- Can you achieve the result with two simple in-place reversals?