Rotate a Square Matrix
Updated
Problem statement
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]]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]]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 <= 1000matrixis square: every row has exactlymatrix.lengthentries.-1000000 <= matrix[i][j] <= 1000000-1000000000 <= quarterTurns <= 1000000000inPlaceis 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 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)