DIY: Rotate Image

Solve the interview question "Rotate Image" in this lesson.

Problem statement

For this problem, you are given a 2D array. You have to implement the function that will rotate the image pixels given in a matrix form clockwise.


  • matrix.length == n
  • matrix[i].length == n
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000


The input of the autorotate function is a nnn*n 2D array. Here is an example of the input:



This is the output of the input given above:


Coding exercise

Implement the autorotate(matrix) function, where the matrix is a 2D array and the function should return the updated matrix.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.