# The Quarter Turn

> One rotation changes everything.

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

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given an n x n matrix, rotate 90 degrees clockwise in place and return it. Element at [i][j] moves to [j][n-1-i].

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **in-place matrix transformation** using the transpose-then-reverse technique. It checks whether candidates can decompose a complex operation (rotation) into simpler operations (transpose + reverse rows).

> **Trick to Solving**
>
> A 90-degree clockwise rotation equals a transpose (swap rows and columns) followed by reversing each row. Both operations are simple to implement in-place and compose cleanly.

---

### Break down the requirements

#### Step 1: Transpose the matrix

Swap `matrix[i][j]` with `matrix[j][i]` for all `i < j`. This mirrors the matrix along the main diagonal.

#### Step 2: Reverse each row

After transposing, reverse each row to complete the clockwise rotation.

---

### The solution

**Transpose then reverse rows for 90-degree rotation**

```python
def rotate(matrix):
    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 Complexity**
>
> **Time:** O(n^2) where n is the matrix dimension. Each element is touched a constant number of times.
> 
> **Space:** O(1). All operations are in-place.

> **Interviewers Watch For**
>
> Can you derive the two-step approach on a whiteboard? Drawing a 3x3 example and showing how transpose + reverse produces the rotated result is the expected walkthrough.

> **Common Pitfall**
>
> Transposing the full matrix (both `(i,j)` and `(j,i)`) which swaps each pair twice, undoing the transpose. Only iterate where `j > i`.

---

## Common follow-up questions

- How would you rotate counter-clockwise? _(Tests transposing then reversing columns (or reversing rows then transposing).)_
- What about rotating 180 degrees? _(Tests reversing all rows then all columns, or applying 90-degree rotation twice.)_
- What if the matrix were not square? _(Tests that in-place rotation only works for square matrices; non-square requires a new matrix.)_
- How would you rotate a single layer (ring) of the matrix? _(Tests the four-way cyclic swap technique used in spiral 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). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.