How do you rotate a matrix by 90 degrees clockwise?

DSA
Medium
Amazon
102.7K views

This problem tests matrix manipulation skills and the ability to perform in-place transformations. It requires understanding row-column index mapping.

Why Interviewers Ask This

Matrix rotation is a common interview problem that checks spatial reasoning and algorithmic precision. It evaluates if candidates can derive the transformation formula and implement it efficiently without extra space.

How to Answer This Question

Explain the two-step process: transpose the matrix (swap rows and columns) and then reverse each row. Alternatively, describe the direct index mapping where element at (i, j) moves to (j, n-1-i). Emphasize doing this in-place to save space.

Key Points to Cover

  • Transpose the matrix first
  • Reverse each row
  • Perform in-place
  • O(N^2) time complexity

Sample Answer

The most efficient way is to first transpose the matrix by swapping elements across the diagonal. After transposing, I reverse each row of the matrix. This effectively rotates the matrix 90 degrees clockwise. This approach runs in O(N^2) time and uses O(1) extra space.

Common Mistakes to Avoid

  • Using extra space for a new matrix
  • Incorrect index mapping
  • Rotating counter-clockwise by mistake

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 35 DSA questionsBrowse all 125 Amazon questions