Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Rotate Image (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an n x n 2D matrix, modify a square matrix by rotating it 90 degrees in a clockwise direction.

Note: This rotation should be done in-place, meaning the transformation must occur within the original matrix without using any additional space for another matrix.

Examples

  • Example 1:
    • Input: matrix =
[[1,2],
 [3,4]]
  • Expected Output:
[[3,1],
 [4,2]]
  • Justification: After rotating the 2x2 matrix 90 degrees to the right, the element at the top left (1) moves to the top right, the top right (2) moves to the bottom right, the bottom right (4) moves to the bottom left, and the bottom left (3) moves to the top left.

  • Example 2:

    • Input: matrix =
[[5,1,9],
 [2,4,8],
 [13,3,6]]
  • Expected Output:
[[13,2,5],
 [3,4,1],
 [6,8,9]]
  • Justification: Rotating the 3x3 matrix 90 degrees to the right repositions the first row to the last column, the second row to the middle column, and the third row to the first column.

  • Example 3:

    • Input: matrix =
[[10,11,12,13],
 [14,15,16,17],
 [18,19,20,21],
 [22,23,24,25]]
  • Expected Output:
[[22,18,14,10],
 [23,19,15,11],
 [24,20,16,12],
 [25,21,17,13]]
  • Justification: The matrix is rotated by 90 degrees in the clockwise direction.

Constraints:

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

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible