# The Quarter Turn

> One rotation changes everything.

Canonical URL: <https://datadriven.io/problems/the_quarter_turn>

Domain: Python · Difficulty: medium · Seniority: L4 · Asked in: Citadel

## Problem

We store map tiles as square grids of pixel values, and the render step turns each one a quarter-turn clockwise before drawing it. Rotate the grid clockwise in place and return the same grid.

## 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 = [[1,2],[3,4]]
```

Output:

```
[[3,1],[4,2]]
```

## Worked solution and explanation

### What this problem is really testing

Strip the map-tile costume and this is a coordinate remap: the value at row `i`, column `j` has to land at row `j`, column `n-1-i`. Anyone can see it's a rotation. The separator between candidates is direction and staging. A transpose alone only mirrors the grid, transpose-then-reverse-columns rotates the wrong way, and writing a cell before reading its old value corrupts the data mid-flight. The clean move is to compose two dead-simple in-place operations: transpose across the main diagonal, then reverse each row.

> **Trick to solving**
>
> A 90-degree clockwise rotation equals a transpose (swap rows and columns) followed by reversing each row. Both operations are trivial to do in place, and they compose without any scratch buffer.

---

### Break down the two moves

#### Step 1: Transpose the grid

Swap `matrix[i][j]` with `matrix[j][i]` for every `j > i`. That mirrors the grid along the main diagonal. Iterating only the upper triangle matters: touch both `(i,j)` and `(j,i)` and you swap each pair twice, undoing the transpose.

#### Step 2: Reverse each row

Reverse each row in place with a two-pointer swap. Transpose put the columns in the right places but top-to-bottom; reversing each row flips them left-to-right to finish the clockwise turn.

---

### The solution

**Transpose, then reverse each row**

```python
def rotate_90_clockwise(matrix: list) -> list:
    n = len(matrix)
    for i in range(n):
        for j in range(i + 1, n):
            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
    for row in matrix:
        left = 0
        right = len(row) - 1
        while left < right:
            row[left], row[right] = row[right], row[left]
            left += 1
            right -= 1
    return matrix
```

> **Time and space**
>
> **Time:** O(n^2) where n is the grid dimension; each cell is touched a constant number of times. **Space:** O(1); every swap happens inside the original grid, so an empty or 1x1 grid falls straight through with no special case.

> **Interviewers watch for**
>
> Whether you settle direction before writing code. Strong candidates ask clockwise versus counterclockwise up front, then sketch a 3x3 and show how transpose plus row-reversal lands the top row into the rightmost column.

> **Common pitfall**
>
> Reversing columns instead of rows after the transpose. That gives a counterclockwise turn, and it passes a symmetric test grid while silently failing every asymmetric one. Trace a non-symmetric 3x3 by hand to catch it.

---

## Common follow-up questions

- How would you rotate counterclockwise instead? _(Tests transposing then reversing columns, or reversing rows then transposing.)_
- What about a 180-degree rotation? _(Tests reversing all rows then all columns, or applying the 90-degree rotation twice.)_
- What changes if the grid is not square? _(Tests recognizing that in-place rotation only works for square grids; a non-square grid needs a fresh matrix of transposed dimensions.)_
- Could you rotate one ring of the grid at a time without the transpose? _(Tests the four-way cyclic corner swap used in spiral and layer problems.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_quarter_turn)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.