Home › Company-wise questions › PayPal › Rotate Image ← PayPal bank
PayPal · Question 9 · Arrays
Rotate Image Arrays Java code ⏱ O(n^2) 💾 O(1)
Product-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Rotate an n x n 2D matrix 90 degrees clockwise in-place.
Approach Transpose the matrix then reverse each row.
Time O(n^2)Space O(1)
How it works 1 Transpose across the diagonal. 2 Reverse 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 == matrix.length; 1 <= n <= 20; in place.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n^2)Space O(1)Loops 4source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE static void rotate(int[][] matrix) DEFINE n = matrix.length for (int i = 0; i < n; i++) for (int j = i; j < n; j++) DEFINE temp = matrix[i][j] matrix[i][j] = matrix[j][i] matrix[j][i] = temp for (int i = 0; i < n; i++) DEFINE left = 0, right = n - 1 while (left < right) DEFINE temp = matrix[i][left] matrix[i][left] = matrix[i][right] matrix[i][right] = temp left++ right-- DEFINE static void main(String[] args) DEFINE [][] m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} rotate(m) System.out.println(Arrays.deepToString(m)) END start / end statement condition loop returns definition← Q8 · Valid Sudoku Q10 · Find All Numbers Disappeared in an Array → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →