# The Grid Pivot

> A different angle reveals a completely different picture.

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

Domain: Python · Difficulty: easy · Seniority: L3 · Asked in: Citadel

## Problem

A metrics export arrives as `matrix`, a list of equal-length rows of readings. Return that grid turned on its side, so each original column becomes a row, and return an empty list when `matrix` is empty.

## Example 1

Input:

```
matrix = [[1,2,3],[4,5,6]]
```

Output:

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

## Example 2

Input:

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

Output:

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

## Worked solution and explanation

### The trap hiding in a rectangle

Strip the costume off and this is a coordinate swap: the value at position (i, j) in the input has to land at (j, i) in the result. Anyone can recite that. What actually separates candidates is noticing the shape CHANGES: an m-by-n grid comes back n-by-m. The people who stumble reach for the textbook square-matrix trick of swapping `matrix[i][j]` with `matrix[j][i]` in place, and that only works when rows equal columns. Hand it the 2-by-3 example and an in-place swap walks straight off the end of a row.

> **Read off columns, not rows**
>
> The result has one row per ORIGINAL column. So the outer loop walks columns of the input, and for each column you gather that column's value from every row. Get the loop nesting backwards and you build the original grid right back, unchanged.

### The solution

**Pivot with zip**

```python
def transpose_matrix(matrix: list) -> list:
    return [list(column) for column in zip(*matrix)]
```

*Unpacking the rows into zip pairs them column-wise; an empty matrix unpacks to nothing, so this returns [] for free.*

`zip(*matrix)` spreads the rows out as separate arguments, then zip stitches the first element of each into one tuple, the second into the next, and so on. That IS the column-wise read you want, with no manual index bookkeeping. The one catch is the output type.

> **zip gives you tuples**
>
> `zip` hands back tuples, not lists. If the contract is a list of lists (and these test cases are), `[(1, 4), (2, 5)]` is the wrong shape and fails an equality check. Wrapping each column in `list(...)` is the difference between right and almost-right.

**In-place square swap**

Swapping `matrix[i][j]` with `matrix[j][i]` for i < j mutates the input and only works when rows == columns. Feed it a rectangle and it indexes out of bounds.

**Build a fresh grid**

Reading columns into new rows handles any m-by-n shape, never touches the input, and is the same O(m*n) work. This is the answer to ship.

> **Interviewers watch for**
>
> Whether you handle non-square matrices. The square-only in-place swap is the classic tell that a candidate memorized one case instead of understanding the operation. State up front that the result is n-by-m and you have already signaled you see it.

> **Cost**
>
> Time is O(m*n): every element is read and placed exactly once. Space is O(m*n) for the new grid. There is no cheaper option when the output must be a separate matrix, so do not over-think micro-optimizations here.

## Common follow-up questions

- Can you transpose in place for a square matrix? _(Tests swapping matrix[i][j] with matrix[j][i] for i < j, and why it is limited to square grids.)_
- What exactly does zip(*matrix) do, and why does it pair things column-wise? _(Tests understanding of argument unpacking and zip's column-wise pairing.)_
- How would you rotate the matrix 90 degrees clockwise instead? _(Tests transpose plus reversing each row (or reverse rows then transpose).)_

## Related

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