Magicsheet logo

Determine Whether Matrix Can Be Obtained By Rotation

Easy
25%
Updated 8/1/2025

Asked by 4 Companies

Determine Whether Matrix Can Be Obtained By Rotation

What is this problem about?

The Determine Whether Matrix Can Be Obtained By Rotation coding problem asks you to check if a square matrix mat can be transformed into another matrix target by rotating it 90 degrees clockwise, zero to three times.

Why is this asked in interviews?

This is a popular question at Microsoft and Meta to test your matrix manipulation skills. Rotating a matrix 2D array involves swapping elements in a specific pattern. It evaluations whether you can perform in-place rotations or whether you can correctly identify the coordinate transformations (i,j)o(j,n1i)(i, j) o (j, n-1-i) for a 90-degree turn.

Algorithmic pattern used

This problem uses Matrix Rotation and Simulation. There are two ways to check:

  1. Simulate and Compare: Rotate mat 90 degrees, compare with target. Repeat up to 4 times.
  2. Coordinate Mapping: For every cell (i,j)(i, j), check if mat[i][j] matches target in any of the four rotation positions:
    • 0 deg: (i,j)(i, j)
    • 90 deg: (j,n1i)(j, n-1-i)
    • 180 deg: (n1i,n1j)(n-1-i, n-1-j)
    • 270 deg: (n1j,i)(n-1-j, i)

Example explanation

Matrix mat:

0 1
1 1

Target target:

1 0
1 1
  1. Original mat eq eq target.
  2. Rotate 90 deg: mat[0][0] o mat[0][1], etc.
    • New mat:
    1 0
    1 1
    
  3. Now mat == target. Result: true.

Common mistakes candidates make

  • Copying errors: Not creating a new matrix for the rotation if not doing it in-place, leading to mixed-up values.
  • Missing 0 Rotation: Forgetting that if the matrices are already equal, it should return true.
  • Incorrect Formula: Swapping ii and jj but forgetting to handle the n1n-1 offset correctly.

Interview preparation tip

The most concise way to rotate a matrix 90 degrees is to Transpose it (swap arr[i][j] with arr[j][i]) and then Reverse each row. This two-step process is much easier to code than direct coordinate swapping.

Similar Questions