Skip to main content

46. Rotate Image

mediumAsked at Datadog

Rotate an N x N matrix 90 degrees clockwise in place. Datadog tests this for the transpose-plus-reverse trick — same shape as their column-store to row-store flip during chunk compaction.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Datadog onsite matrix question.

Problem

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Constraints

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

Examples

Example 1

Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[[7,4,1],[8,5,2],[9,6,3]]

Example 2

Input
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output
[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

Approaches

1. Allocate a new matrix

new[j][n-1-i] = old[i][j]. Then copy back.

Time
O(n^2)
Space
O(n^2)
// Allocate result[n][n]; result[j][n-1-i] = matrix[i][j]; copy back.

Tradeoff: Violates the in-place constraint. Datadog will fail this.

2. Transpose then reverse each row (optimal)

Step 1: swap matrix[i][j] with matrix[j][i] for i < j. Step 2: reverse each row.

Time
O(n^2)
Space
O(1)
function rotate(matrix) {
  const n = matrix.length;
  // Transpose
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
    }
  }
  // Reverse each row
  for (let i = 0; i < n; i++) {
    matrix[i].reverse();
  }
}

Tradeoff: Two passes, O(1) extra space. Datadog-canonical: the transpose+reverse decomposition is the same trick they use for in-place storage layout flips.

Datadog-specific tips

Datadog grades on whether you decompose into two named operations (transpose + reverse) rather than computing the rotation directly. The decomposition is easier to reason about, easier to test, and easier to generalize (counterclockwise = reverse first, then transpose).

Common mistakes

  • Looping j from 0 in the transpose — swaps each pair TWICE (undoing).
  • Forgetting the reverse step — produces a transpose, not a rotation.
  • Trying to rotate in concentric rings — works but is harder to get right than transpose+reverse.

Follow-up questions

An interviewer at Datadog may pivot to one of these next:

  • Rotate counterclockwise — reverse rows first, then transpose.
  • Rotate by 180 — reverse all rows, then reverse all columns.
  • Spiral Matrix (LC 54) — different traversal but same in-place mindset.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why j > i in the transpose loop?

j > i visits each pair exactly once. If you used j > 0 instead, you'd swap each pair twice, undoing the work.

Could you do it as concentric rings?

Yes — for each ring, rotate 4 elements at a time. Same O(n^2) but harder to write. Transpose+reverse is the canonical form.

Practice these live with InterviewChamp.AI

Drill Rotate Image and other Datadog interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →