Rotate a Matrix In Place
AppleMachine Learning Engineer · Technical ScreenLast updated
This question evaluates proficiency in in-place matrix and array manipulation, spatial data transformation, and reasoning about time and space complexity.
Given an n x n matrix of integers, rotate the matrix 90 degrees clockwise.
Implement the function in Python. You may use a straightforward approach with loops, but the preferred solution should rotate the matrix in place without allocating another n x n matrix.
Example:
Input:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Output:
[
[7, 4, 1],
[8, 5, 2],
[9, 6, 3]
]
Discuss the time and space complexity of your approach.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.