FastPrepRotate a Square Image
Problems/Rotate a Square Image

Rotate a Square Image

Morgan Stanley logoMorgan Stanley● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in the final technical and HR round of a Morgan Stanley intern onsite. The author links the Rotate Image problem. The square input, the clockwise quarter turn, and the change in place follow that problem. We chose the number limits and returning the changed matrix so it can be checked.

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

Given a square integer matrix matrix with n rows and n columns, rotate its image by 90 degrees clockwise. An original cell at row r, column c moves to row c, column n-1-r.

Modify the supplied matrix in place using constant extra working memory. Do not allocate another matrix to compute the rotation. For this exercise, return the modified matrix so the judge can observe the mutation. The returned representation is only an observation adapter.

Function

rotateImage(matrix: int[][]) → int[][]

Examples

Example 1

matrix = [[2,4,6],[8,10,12],[14,16,18]]return = [[14,8,2],[16,10,4],[18,12,6]]
matrix
01202461810122141618
→
return
01201482116104218126
Moved or new

The original first column [2,8,14] becomes the top row in reverse order [14,8,2]. Applying the same clockwise move to all cells gives [[14,8,2],[16,10,4],[18,12,6]].

Example 2

matrix = [[-1,2],[3,-4]]return = [[3,-1],[-4,2]]
matrix
010-1213-4
→
return
0103-11-42
Moved or new

The clockwise destination rule sends -1 to the top-right, 2 to the bottom-right, 3 to the top-left and -4 to the bottom-left, giving [[3,-1],[-4,2]].

Constraints

  • For this exercise, assume 1 <= matrix.length <= 20.
  • For this exercise, assume every row has between 1 and 20 entries.
  • The matrix is square: every row length equals matrix.length.
  • For this exercise, assume -1000 <= matrix[r][c] <= 1000.

More Morgan Stanley Onsite Interview questions

  • Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
  • Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
  • Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
  • Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
  • Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
  • Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
  • LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
  • All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
  • Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Minimum Time to Rot All OrangesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
  • Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions

Interview reports that mention this question

  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)

Morgan Stanley interview experiences

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
  • Morgan Stanley New Grad Technology Analyst: 2 Experiences
  • Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
  • Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public int[][] rotateImage(int[][] matrix) {
        // Modify matrix in place and return it.
    }

}
Checking account…