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.size() == n
  • matrix[i].size() == n
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

Input

The input of the autoRotate() function is a n×nn \times 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 the matrix represents a 2D array. The function should return a matrix.

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