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.

Constraints

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

Input

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

[[1,2,3],[4,5,6],[7,8,9]]

Output

This is the output of the input given above:

[[7,4,1],[8,5,2],[9,6,3]]

Coding exercise

Implement the Autorotate(matrix) function, where it takes a 2D array i.e., matrix as input, and returns the matrix.

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