An edge camera box rotates square stills 90° clockwise before upload so the warehouse aisle is upright. The first build allocated a second n × n buffer, wrote next[c][n - 1 - r] = matrix[r][c], and copied back. On the 4K stills that buffer was the OOM. The still has to turn in the same array. Extra matrix is honest and too fat.
Rotate Image asks for a 90° clockwise turn, in place. An array of rows already holds the pixels. The mapping (r, c) → (c, n - 1 - r) is the whole geometry. You either pay a second grid or you layer two reflections that use O(1) extra cells.
The problem
Given an n × n int[][] matrix, rotate it 90° clockwise. Modify matrix itself. Do not allocate a second n × n array as the working copy.
2 8 1 4 5 2
5 3 9 → 7 3 8
4 7 6 6 9 1
(r, c) lands at (c, n - 1 - r)
Note: Counterclockwise is a different pair of reflections. Do not mix the two at the board. Clockwise is transpose, then reverse each row.
Copy into a second matrix
Write every cell to its destination, then copy the buffer back so the caller’s reference still points at the same object. Correct. Extra O(n²) space, which the prompt forbade.
void rotateCopy(int[][] matrix) {
int n = matrix.length;
int[][] next = new int[n][n];
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
next[c][n - 1 - r] = matrix[r][c];
}
}
for (int r = 0; r < n; r++) {
System.arraycopy(next[r], 0, matrix[r], 0, n);
}
}
At n = 3 this is a rounding error. At n in the thousands you paid a second image for a question two in-place reflections already answer: transpose, then reverse each row — in the same array.
Transpose, then reverse each row
A clockwise quarter-turn is a reflection across the main diagonal, then a reversal of every row. Transpose swaps matrix[r][c] with matrix[c][r] for c > r so you do not swap twice. Then two indexes walk each row inward.
after transpose
2 5 4
8 3 7
1 9 6
after each row reversed
4 5 2
7 3 8
6 9 1
The Java is those two layers:
void rotate(int[][] matrix) {
int n = matrix.length;
for (int r = 0; r < n; r++) {
for (int c = r + 1; c < n; c++) {
int tmp = matrix[r][c];
matrix[r][c] = matrix[c][r];
matrix[c][r] = tmp;
}
}
for (int r = 0; r < n; r++) {
int lo = 0;
int hi = n - 1;
while (lo < hi) {
int tmp = matrix[r][lo];
matrix[r][lo] = matrix[r][hi];
matrix[r][hi] = tmp;
lo++;
hi--;
}
}
}
Time is O(n²) — every cell moves a constant number of times. Extra space is O(1) besides the input; a few int temps, no second grid.
Note: Transpose must walk only one triangle. If the inner loop starts at c = 0, every off-diagonal pair swaps twice and you are back where you started. Reverse-rows-then-transpose is counterclockwise. Keep the order.
What interviewers usually poke next
- Four-cell cycle. For each layer,
tmp = (r, c), then(r, c) ← (n - 1 - c, r)around the square. SameO(1)extra space, one loop nest, easier to get an index wrong. Name it as the other in-place bill. - Counterclockwise. Reverse each row, then transpose — or transpose, then reverse each column. Do not reuse the clockwise pair.
- 180°. Two clockwise turns, or reverse the row order and reverse each row. Say which you mean.
- Non-square. The prompt is
n × n. Anm × nrotate needs a new array; in place is the wrong shape. At the board, ask.
You are done with this problem when you can walk the 3×3 on a whiteboard through transpose then row reverse, and you can say out loud why a second matrix is the honest brute and why the inner transpose loop starts at c = r + 1.