FastPrepRotate Image
Problems/Rotate Image

Rotate Image

Tokopedia logoTokopedia● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names and links the well-known problem. Your function returns the matrix, which only shows the in-place result so it can be checked. Our version is about 99% the same as the reported question.

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]]
matrix
012012314562789
→
return
012074118522963
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]]
matrix
0123051911124810213367315141216
→
return
0123015132511434121268931671011
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 Tokopedia interview questions

  • Interleaving StringPHONE SCREEN · Seen Jul 2021
  • Delete Node in a BSTPHONE SCREEN · Seen Jul 2021
  • Validate Binary Search TreeONSITE INTERVIEW · Seen Aug 2021
  • Kth Largest Element in an ArrayONSITE INTERVIEW · Seen Jul 2021
  • Online Stock SpanONSITE INTERVIEW · Seen Jul 2021
  • Vertical Order Traversal of a Binary TreeONSITE INTERVIEW · Seen Jul 2021

Similar questions at other companies

  • Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
  • Linear, Sigmoid and L2 Forward and Backward PassesNVIDIA · PHONE SCREENNVIDIA interview questions
  • Rotate ImageSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
  • Scaled Dot-Product AttentionAmazon · PHONE SCREEN
  • Multiply Two MatricesAdobe · PHONE SCREENAdobe interview questions
  • Adjacent Order-Statistic Gap DistributionsScale AI · PHONE SCREENScale AI interview questions
  • Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
  • Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
  • Build a Gaussian Blur KernelLuma AI · OALuma AI interview questions
  • Rotate a Square Grid ClockwiseC3.ai · ONSITE INTERVIEWC3.ai interview questions
All Tokopedia interview questionsSee Tokopedia hiring insights
Saved
class Solution {
    public int[][] rotateImage(int[][] matrix) {
        // Rotate matrix in place, then return it.
    }
}
Checking account…