FastPrepRotate a Square Matrix
Problems/Rotate a Square Matrix

Rotate a Square Matrix

Akuna Capital logoAkuna Capital● MediumNEW GRADINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reports cover clockwise and counterclockwise rotations, rotating by several quarter turns, copy and in-place versions, and a follow-up that does it in a single pass. Our function combines these versions with a mode flag. We chose these rules: the matrix is square, the numbers have limits, turns are written as signed numbers, and an empty matrix has a defined result. Checking the input is left as a discussion follow-up.

Our version is about 90% the same as the reported questions.

Rotate the square integer matrix matrix by quarterTurns quarter-turns. One positive quarter-turn is 90 degrees clockwise; a negative turn rotates counterclockwise. Four quarter-turns restore the original orientation.

The flag inPlace selects the required behavior. If it is true, update the supplied matrix and return its rotated contents. If it is false, leave the supplied matrix unchanged and return a new matrix with independent rows. An empty matrix returns an empty matrix.

Handle any allowed turn count directly after reducing it modulo four, including 180 and 270 degrees. For the in-place transformation, use constant extra working space rather than a second matrix; the returned contents are the output. In the copy version, allocate the new output matrix.

Practice follow-up: describe the shape and value checks needed for general nested input. No return value is defined for malformed input; only valid inputs below are graded.

Function

rotateMatrix(matrix: int[][], quarterTurns: int, inPlace: boolean) → int[][]

Examples

Example 1

matrix = [[1,2,3],[4,5,6],[7,8,9]]quarterTurns = 1inPlace = falsereturn = [[7,4,1],[8,5,2],[9,6,3]]
matrix
012012314562789
→
return
012074118522963
Moved or new

One clockwise turn makes the original left column, read from bottom to top, the new top row: [7,4,1]. The other new rows are [8,5,2] and [9,6,3]. The supplied matrix stays unchanged because inPlace is false.

Example 2

matrix = [[1,2],[3,4]]quarterTurns = -1inPlace = truereturn = [[2,4],[1,3]]
matrix
01012134
→
return
01024113
Moved or new

Minus one quarter-turn means 90 degrees counterclockwise. The original right column [2,4] becomes the top row. Update the supplied matrix to [[2,4],[1,3]] and return those contents.

Example 3

matrix = []quarterTurns = 2inPlace = truereturn = []

The empty matrix has no entries to move, so its rotated contents are still an empty matrix.

Constraints

  • 0 <= matrix.length <= 1000
  • matrix is square: every row has exactly matrix.length entries.
  • -1000000 <= matrix[i][j] <= 1000000
  • -1000000000 <= quarterTurns <= 1000000000
  • inPlace is a Boolean.

More Akuna Capital Phone Screen questions

  • Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
  • Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Two SumOA · PHONE SCREEN · Seen Sep 2026
  • Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
  • Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
  • Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
  • Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
  • Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
  • Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025

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
  • Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
  • 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
  • Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions

Interview reports that mention this question

  • Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)

Akuna Capital interview experiences

  • Akuna Capital Quant Trader and QR Interviews: 5 Experiences
  • Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
  • Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
  • Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
  • Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)
All Akuna Capital interview questionsSee Akuna Capital hiring insights
Saved
class Solution {
    public int[][] rotateMatrix(int[][] matrix, int quarterTurns, boolean inPlace) {
        // write your code here
    }
}
Checking account…