Rotate a Flat Image Clockwise
Updated
Problem statement
A grayscale image with rows rows and cols columns is stored as one flat array pixels, row by row: the pixel at row r and column c is pixels[r * cols + c].
Rotate the image 90 degrees clockwise and return it in the same flat, row-by-row format. The rotated image has cols rows and rows columns.
Function
rotateFlatImage(pixels: int[], rows: int, cols: int) → int[]Examples
Example 1
pixels = [1,2,3,4,5,6]rows = 2cols = 3return = [4,1,5,2,6,3]The image is [[1, 2, 3], [4, 5, 6]]. Turned a quarter clockwise it becomes [[4, 1], [5, 2], [6, 3]]: 3 rows of 2. Read row by row, that is [4, 1, 5, 2, 6, 3].
Example 2
pixels = [1,2,3,4,5,6,7,8,9]rows = 3cols = 3return = [7,4,1,8,5,2,9,6,3][[1, 2, 3], [4, 5, 6], [7, 8, 9]] rotates to [[7, 4, 1], [8, 5, 2], [9, 6, 3]].
Example 3
pixels = [10,20,30]rows = 1cols = 3return = [10,20,30]One row of 3 pixels becomes one column of 3 pixels, so the values keep their order: [10, 20, 30].
Constraints
1 <= rows, colsandrows * cols <= 2000pixels.length == rows * cols0 <= pixels[i] <= 255
More Google Onsite Interview questions
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Rotate a Square Grid ClockwiseC3.ai · ONSITE INTERVIEWC3.ai interview questions
- Survey Table: Group Given an AnswerSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Multiply Two MatricesAdobe · PHONE SCREENAdobe interview questions
- Matrix Multiplication With and Without NumPyNVIDIA · PHONE SCREENNVIDIA interview questions
- Adjacent Order-Statistic Gap DistributionsScale AI · PHONE SCREENScale AI interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Build a Gaussian Blur KernelLuma AI · OALuma AI interview questions