FastPrepRotate a Flat Image Clockwise
Problems/Rotate a Flat Image Clockwise

Rotate a Flat Image Clockwise

Google logoGoogle● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In a fall 2026 Google AI/ML SWE L4 coding round, the input was a flat array holding an image, along with its height and width. The candidate had to turn the image a quarter turn clockwise. The round ended with a talk about speeding up the solution, and no follow-up question was asked.

The report does not give the storage order, value range, or limits. Here, the array is row by row, the result uses the same layout, and pixel values are 0 to 255. The talk about efficiency is in the editorial and is not checked. The limits are our choice.

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

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, cols and rows * cols <= 2000
  • pixels.length == rows * cols
  • 0 <= 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

Interview reports that mention this question

  • Google SWE III ML Experienced Hires: 34 Experiences (Part 1 of 4)

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
  • Google SWE Intern Interview Results Timeline: 13 Experiences
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] rotateFlatImage(int[] pixels, int rows, int cols) {
        // Write your code here.
    }
}
Checking account…