Rotate a Matrix by 90 Degrees
Updated
Problem statement
Given an n × n integer matrix, rotate it 90 degrees clockwise in place and return the modified matrix.
The original value at row r and column c moves to row c and column n - 1 - r. Rows and columns use zero-based indices.
Keep the matrix square and preserve every value. An approach using temporary storage is valid; aim to reduce the extra space to O(1).
Function
rotateMatrix(matrix: int[][]) → int[][]Examples
Example 1
matrix = [[1,2],[3,4]]return = [[3,1],[4,2]]Moved or new
The original bottom-left value 3 moves to the top-left, and the original top-right value 2 moves to the bottom-right.
Example 2
matrix = [[2,5,8],[1,4,7],[0,3,6]]return = [[0,1,2],[3,4,5],[6,7,8]]Moved or new
Each original column becomes a row, read from bottom to top.
Constraints
1 <= n <= 40.- Every row contains exactly
nintegers. - Every value is in
[-10^6, 10^6].
More Mygate Phone Screen questions
- Employees Earning More Than Their ManagerPHONE SCREEN · Seen Sep 2026
- Maximum Subarray SumPHONE SCREEN · Seen Sep 2026
- Reverse a Linked ListPHONE SCREEN · Seen Sep 2026
- Users Active in Both January and FebruaryPHONE SCREEN · Seen Sep 2026
- Elements Occurring More Than n/3 TimesPHONE SCREEN · Seen Jan 2025
- Frog Jump: Minimum EnergyPHONE SCREEN · Seen Jan 2025
- Left View of a Binary TreePHONE SCREEN · Seen Jan 2025
- Two SumPHONE SCREEN · Seen Aug 2024
- Minimum in a Rotated Sorted ArrayPHONE SCREEN · Seen Jul 2024
- Minimum Subarray SumPHONE SCREEN · Seen Sep 2023
Similar questions at other companies
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Sparse Matrix MultiplicationByteDance · PHONE SCREENByteDance interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN