# The Spin Doctor

> Ninety degrees, but which way?

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

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given an n x n matrix, return a new matrix rotated 90 degrees counter-clockwise. Do not mutate the input.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **matrix transformation** and index manipulation. 90-degree counter-clockwise rotation requires understanding the coordinate mapping between source and destination positions, which probes spatial reasoning.

> **Trick to Solving**
>
> For a 90-degree counter-clockwise rotation, element at position `[i][j]` moves to `[n-1-j][i]`. You can also achieve this by transposing the matrix and then reversing each column (or equivalently, reversing each row of the transpose reversed).

---

### Break down the requirements

#### Step 1: Determine the dimension n

The matrix is n x n. The output has the same dimensions.

#### Step 2: Map each element to its rotated position

For counter-clockwise 90 degrees: the first row becomes the last column, the last row becomes the first column.

#### Step 3: Build a new matrix without modifying the input

Allocate a fresh n x n matrix and populate it using the coordinate mapping.

---

### The solution

**Direct index mapping for counter-clockwise rotation**

```python
def rotate_ccw(matrix: list) -> list:
    n = len(matrix)
    rotated = []
    for i in range(n):
        row = []
        for j in range(n):
            row.append(matrix[j][n - 1 - i])
        rotated.append(row)
    return rotated
```

> **Time and Space Complexity**
>
> **Time:** O(n^2) to visit every element once.
> 
> **Space:** O(n^2) for the new matrix. The input is not modified.

> **Interviewers Watch For**
>
> Can you derive the index formula yourself, or do you need to memorize it? Strong candidates work through a 2x2 or 3x3 example to verify the mapping.

> **Common Pitfall**
>
> Confusing clockwise and counter-clockwise rotations. Clockwise maps `[i][j]` to `[j][n-1-i]`, while counter-clockwise maps to `[n-1-j][i]`. Mixing these up produces the wrong rotation.

---

## Common follow-up questions

- How would you rotate the matrix in-place? _(Tests layer-by-layer swapping with four-way element cycling.)_
- What about rotating by 180 degrees? _(Tests recognizing this is two 90-degree rotations or a simpler reverse-rows-and-columns approach.)_
- How would you handle a non-square (m x n) matrix? _(Tests that the output dimensions become n x m and the index formula changes.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_spin_doctor)
- [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.