Gamified challenge · 2-D index mapping
photo is a square n × n grid of pixel values. Turn it 90° clockwise: the left column becomes the top row, the top row becomes the right column, and so on. In other words, the pixel at (r, c) moves to (c, n - 1 - r).
Write rotate(photo) that turns the photo and returns it. Try to do it in place, with O(1) extra memory, then return the same grid. Example: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] → [[7, 4, 1], [8, 5, 2], [9, 6, 3]].
An empty photo [] stays []. Pixel values are any whole numbers.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google