Rotate a Square Matrix Clockwise
Updated
Problem statement
You are given an n x n integer matrix. Return a new matrix obtained by rotating the input exactly 90 degrees clockwise.
The returned cell at row r and column c must contain the original value from row n - 1 - c and column r. You do not need to mutate the input matrix.
Function
rotateSquareMatrixClockwise(matrix: int[][]) → int[][]Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]return = [[7,4,1],[8,5,2],[9,6,3]]Moved or new
The first column becomes the first row in reverse order, and the same rule applies to every column.
Example 2
matrix = [[5]]return = [[5]]A one-cell matrix is unchanged by rotation.
Example 3
matrix = [[-1,2],[3,4]]return = [[3,-1],[4,2]]Moved or new
The transformation preserves every signed value while moving it to its clockwise position.
Constraints
1 <= n <= 500.matrix.length = nand every row has lengthn.- Every matrix value is a signed 32-bit integer.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026
Similar questions at other companies
- Maximum Diamond SumHudson River Trading · OAHudson River Trading interview questions
- Robot Navigation Around LasersRoblox · OARoblox interview questions
- Apply a Reversi MoveAmazon · OA
- Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
- First Completed Mingo LineEpic · OAEpic interview questions
- Sort Concentric Matrix Borders ClockwiseByteDance · OAByteDance interview questions
- Sparse Valid Conv2D From ScratchNVIDIA · OANVIDIA interview questions
- Apply Matrix OperationsCapital One · OACapital One interview questions
- Rotate the Box and Drop the StonesZipRecruiter · OAZipRecruiter interview questions
- 2048 Move SimulatorBenchling · OABenchling interview questions