{"id":"6b093ddfbd4755deb67ee095","slug":"rotate-image","title":"Rotate an image by 90°","summary":"Rotate a square pixel matrix 90° clockwise – in place, without a copy. Transpose and mirror, or swap elements four at a time.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"easy","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["arrays","matrix","in-place"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** Rotates the square matrix 90° clockwise, in place. Returns nothing. */\nfunction rotate(matrix: number[][]): void {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('3 × 3', () => {\n  const m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];\n  rotate(m);\n  expect(m).toEqual([[7, 4, 1], [8, 5, 2], [9, 6, 3]]);\n});\n\ntest('2 × 2 and 1 × 1', () => {\n  const a = [[1, 2], [3, 4]];\n  rotate(a);\n  expect(a).toEqual([[3, 1], [4, 2]]);\n  const b = [[5]];\n  rotate(b);\n  expect(b).toEqual([[5]]);\n});\n\ntest('empty matrix', () => {\n  const m = [];\n  rotate(m);\n  expect(m).toEqual([]);\n});\n\ntest('4 × 4', () => {\n  const m = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]];\n  rotate(m);\n  expect(m).toEqual([[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]);\n});\n\ntest('works in place and returns nothing', () => {\n  const m = [[1, 2], [3, 4]];\n  const result = rotate(m);\n  expect(result).toBeUndefined();\n  expect(m).toEqual([[3, 1], [4, 2]]);\n});\n\ntest('four rotations give the original matrix', () => {\n  const m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];\n  for (let i = 0; i < 4; i++) rotate(m);\n  expect(m).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);\n});\n\ntest('performance: 500 × 500', () => {\n  const n = 500;\n  const m = Array.from({ length: n }, (_, r) => Array.from({ length: n }, (_, c) => r * n + c));\n  rotate(m);\n  let ok = true;\n  for (let r = 0; r < n && ok; r++) for (let c = 0; c < n; c++) if (m[c][n - 1 - r] !== r * n + c) { ok = false; break; }\n  expect(ok).toBe(true);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Rotates the square matrix 90° clockwise, in place. Returns nothing.\n * @param {number[][]} matrix\n */\nfunction rotate(matrix) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"test('3 × 3', () => {\n  const m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];\n  rotate(m);\n  expect(m).toEqual([[7, 4, 1], [8, 5, 2], [9, 6, 3]]);\n});\n\ntest('2 × 2 and 1 × 1', () => {\n  const a = [[1, 2], [3, 4]];\n  rotate(a);\n  expect(a).toEqual([[3, 1], [4, 2]]);\n  const b = [[5]];\n  rotate(b);\n  expect(b).toEqual([[5]]);\n});\n\ntest('empty matrix', () => {\n  const m = [];\n  rotate(m);\n  expect(m).toEqual([]);\n});\n\ntest('4 × 4', () => {\n  const m = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]];\n  rotate(m);\n  expect(m).toEqual([[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]);\n});\n\ntest('works in place and returns nothing', () => {\n  const m = [[1, 2], [3, 4]];\n  const result = rotate(m);\n  expect(result).toBeUndefined();\n  expect(m).toEqual([[3, 1], [4, 2]]);\n});\n\ntest('four rotations give the original matrix', () => {\n  const m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];\n  for (let i = 0; i < 4; i++) rotate(m);\n  expect(m).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);\n});\n\ntest('performance: 500 × 500', () => {\n  const n = 500;\n  const m = Array.from({ length: n }, (_, r) => Array.from({ length: n }, (_, c) => r * n + c));\n  rotate(m);\n  let ok = true;\n  for (let r = 0; r < n && ok; r++) for (let c = 0; c < n; c++) if (m[c][n - 1 - r] !== r * n + c) { ok = false; break; }\n  expect(ok).toBe(true);\n});\n"},{"lang":"python","runner":"browser","starter":"def rotate(matrix: list[list[int]]) -> None:\n    \"\"\"Rotates the square matrix 90 degrees clockwise, in place. Returns nothing.\"\"\"\n    raise NotImplementedError\n","testCount":7,"tests":"def check(matrix, expected):\n    result = rotate(matrix)\n    assert result is None, f\"rotate() should return None, got {result!r}\"\n    assert matrix == expected, f\"expected {expected}, got {matrix}\"\n\n\ndef test_3x3():\n    check([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[7, 4, 1], [8, 5, 2], [9, 6, 3]])\n\n\ndef test_2x2_and_1x1():\n    check([[1, 2], [3, 4]], [[3, 1], [4, 2]])\n    check([[5]], [[5]])\n\n\ndef test_empty_matrix():\n    check([], [])\n\n\ndef test_4x4():\n    check([[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]],\n          [[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]])\n\n\ndef test_in_place_same_row_objects_allowed():\n    m = [[1, 2], [3, 4]]\n    alias = m\n    rotate(m)\n    assert alias == [[3, 1], [4, 2]], f\"the caller's matrix was not modified: {alias}\"\n\n\ndef test_four_rotations_give_the_original():\n    m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n    for _ in range(4):\n        rotate(m)\n    assert m == [[1, 2, 3], [4, 5, 6], [7, 8, 9]], f\"after four rotations: {m}\"\n\n\ndef test_performance_500x500():\n    n = 500\n    m = [[r * n + c for c in range(n)] for r in range(n)]\n    rotate(m)\n    for r in range(n):\n        row = m[r]\n        for c in range(n):\n            if row[c] != (n - 1 - c) * n + r:\n                raise AssertionError(f\"wrong value at ({r}, {c}): {row[c]}\")\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>Rotates the square matrix 90 degrees clockwise, in place.</summary>\n    public static void Rotate(int[][] matrix)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":6},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /** Rotates the square matrix 90 degrees clockwise, in place. */\n    static void rotate(int[][] matrix) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":6},{"lang":"go","runner":"judge0","starter":"package main\n\n// Rotate rotates the square matrix 90 degrees clockwise, in place.\nfunc Rotate(matrix [][]int) {\n\tpanic(\"not implemented\")\n}\n","testCount":6}],"questionCount":null,"xp":10,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:55.397Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"A photo app rotates thumbnails 90° to the right. An image is a square "},{"type":"text","text":"n × n","marks":[{"type":"code"}]},{"type":"text","text":" matrix of numbers (pixels), and it has to be rotated "},{"type":"text","text":"in place","marks":[{"type":"bold"}]},{"type":"text","text":" – without a second matrix, because there are many thumbnails and the device memory is limited."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"paragraph","content":[{"type":"text","text":"The function modifies the given matrix so that afterwards it holds the image rotated 90° clockwise. It returns nothing. The element at "},{"type":"text","text":"(r, c)","marks":[{"type":"code"}]},{"type":"text","text":" moves to "},{"type":"text","text":"(c, n - 1 - r)","marks":[{"type":"code"}]},{"type":"text","text":"."}]},{"type":"paragraph","content":[{"type":"text","text":"Signatures: "},{"type":"text","text":"rotate(matrix)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"rotate(matrix)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.Rotate","marks":[{"type":"code"}]},{"type":"text","text":" (C#, "},{"type":"text","text":"int[][]","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"Solution.rotate","marks":[{"type":"code"}]},{"type":"text","text":" (Java, "},{"type":"text","text":"int[][]","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"Rotate","marks":[{"type":"code"}]},{"type":"text","text":" (Go)."}]},{"type":"heading","attrs":{"level":2,"id":"examples"},"content":[{"type":"text","text":"Examples"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Before"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"After"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[1,2,3],[4,5,6],[7,8,9]]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[7,4,1],[8,5,2],[9,6,3]]","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[1,2],[3,4]]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[3,1],[4,2]]","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[5]]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[[5]]","marks":[{"type":"code"}]}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"0 ≤ n ≤ 500","marks":[{"type":"code"}]},{"type":"text","text":", the matrix is square."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"The tests check "},{"type":"text","text":"the same","marks":[{"type":"bold"}]},{"type":"text","text":" matrix object that was passed to the function."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"O(n²)","marks":[{"type":"bold"}]},{"type":"text","text":" time, "},{"type":"text","text":"O(1)","marks":[{"type":"bold"}]},{"type":"text","text":" extra memory."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/rotate-image/","title":"LeetCode 48 – Rotate Image","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:55.398Z","updatedAt":"2026-11-27T08:22:55.398Z"}