Accenture · Question 12 · Arrays
Rotate Image (90 degrees)
Problem
Rotate an n×n 2D matrix by 90 degrees clockwise in - place.
Approach
First transpose the matrix, then reverse each row.
Time O(n²)Space O(1)
How it works
- 1Transpose across the diagonal.
- 2Reverse every row.
Sample input / output
Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
Constraints
n x n matrix; 1 <= n <= 20; in place.
Solution — read it, then walk it
Time O(n²)Space O(1)Loops 5Branches 2Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/27
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits