FastPrepRotate a Matrix by 90 Degrees
Problems/Rotate a Matrix by 90 Degrees

Rotate a Matrix by 90 Degrees

IDFC Bank logoIDFC Bank● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the technical round of an in-person IDFC FIRST Bank hiring drive for a Senior Software Developer, the panel asked for pseudocode that rotates an N by M matrix by 90 degrees.

The report does not say which way to turn, and gives no limits or examples. Here, the matrix turns clockwise, a new matrix with M rows and N columns is returned, and both sides are at most 50. You write real code instead of pseudocode.

Our version is about 85% the same as the reported question.

You are given a matrix matrix with n rows and m columns. Rotate it 90 degrees clockwise and return the rotated matrix.

The rotated matrix has m rows and n columns. The first row of matrix becomes the last column of the result, the second row becomes the second-to-last column, and so on. Because the shape can change, return a new matrix instead of changing matrix in place.

Function

rotateMatrix(matrix: int[][]) → int[][]

Examples

Example 1

matrix = [[1,2,3],[4,5,6]]return = [[4,1],[5,2],[6,3]]
matrix
01201231456
→
return
01041152263

The input has 2 rows and 3 columns, so the result has 3 rows and 2 columns. The first row [1,2,3] becomes the last column, read from top to bottom. The second row [4,5,6] becomes the first column.

Example 2

matrix = [[1,2],[3,4]]return = [[3,1],[4,2]]
matrix
01012134
→
return
01031142
Moved or new

For a square matrix the shape stays the same. Row [1,2] becomes the right column and row [3,4] becomes the left column.

Example 3

matrix = [[7],[8],[9]]return = [[9,8,7]]
matrix
0071829
→
return
0120987

A single column of 3 rows becomes a single row. The bottom value 9 comes first, because the last row of the input becomes the first column of the result.

Constraints

  • n == matrix.length
  • m == matrix[i].length
  • 1 <= n, m <= 50
  • -1000 <= matrix[i][j] <= 1000
See IDFC Bank hiring insights
Saved
class Solution {
    public int[][] rotateMatrix(int[][] matrix) {
        // Write your code here.
    }
}
Checking account…