Rotate a Square Matrix 90 Degrees Clockwise
Updated
Problem statement
You are given an n × n integer matrix that represents an image. Rotate the image by 90 degrees clockwise.
Do the rotation in place by changing matrix directly. Return that same matrix afterwards so the answer can be checked. In your final approach, do not create another n × n matrix.
Function
rotateImage(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, read from the bottom up, is seven, four, one. It becomes the first row. Every other column turns into a row in the same way.
Example 2
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]return = [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]Moved or new
The first column, read from the bottom up, is fifteen, thirteen, two, five. It becomes the first row. Each column turns into a row, reading bottom to top.
Constraints
n == matrix.length == matrix[i].length.1 <= n <= 20.-1000 <= matrix[i][j] <= 1000.
More ZipRecruiter Online Assessment questions
- Four-Level Banking System with Scheduled PaymentsOA · Seen Oct 2026
- Count Three-Digit Numbers with Distinct DigitsOA · Seen Oct 2024
- Longest Frequency-Bounded SubarrayOA · Seen Oct 2024
- Minimum Changes to Draw a YOA · Seen Oct 2024
- Apply Binary State OperationsOA · Seen Sep 2024
- Simulate Orchard Rot SpreadOA · Seen Sep 2024
- Compare Odd- and Even-Index SumsOA · Seen Sep 2024
- Coordinate Removals with Column GravityOA · Seen Sep 2024
- Lexicographically Smallest End ReversalOA · Seen Sep 2024
- Recursively Expanded Shell Command CountsOA · Seen Sep 2024
Similar questions at other companies
- Build a Gaussian Blur KernelLuma AI · OALuma AI interview questions
- Survey Table: Group Given an AnswerSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Rotate a Square Grid ClockwiseC3.ai · ONSITE INTERVIEWC3.ai interview questions
- Linear, Sigmoid and L2 Forward and Backward PassesNVIDIA · PHONE SCREENNVIDIA interview questions
- Rotate ImageTokopedia · PHONE SCREENTokopedia interview questions
- Rotate ImageSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions