FastPrepRotate a Rectangular Matrix Clockwise
Problems/Rotate a Rectangular Matrix Clockwise

Rotate a Rectangular Matrix Clockwise

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source directly provides a 90-degree rotation of any n-by-m matrix, explicitly notes that n need not equal m, and asks about O(1) space and a closed-form solution. Clockwise direction follows the LeetCode 48 reference and the author-confirmed generalization. Return encoding, examples, bounds, and the convention that output storage is excluded from auxiliary-space accounting are practice scaffolding. The judged core matches the visible source at about 95%.

Given a nonempty rectangular integer matrix with m rows and n columns, return its 90-degree clockwise rotation.

The returned matrix has n rows and m columns. Use only O(1) auxiliary space beyond the required output matrix.

Function

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

Examples

Example 1

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

The two rows become the two columns read from bottom to top.

Example 2

matrix = [[1],[2],[3]]return = [[3,2,1]]

A 3x1 column becomes a 1x3 row.

Example 3

matrix = [[7,8,9,10]]return = [[7],[8],[9],[10]]

A 1x4 row becomes a 4x1 column.

Constraints

  • 1 <= matrix.length, matrix[0].length <= 300.
  • Every row has the same length.
  • -1000000000 <= matrix[i][j] <= 1000000000.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
public int[][] rotateRectangularMatrix(int[][] matrix) {
    // write your code here
}
Checking account…