← Questions

Transpose a matrix and reason about memory layout

0
NvidiaCodingLast asked Apr 2026

A coding interview question asked in Nvidia's coding round, last asked Apr 2026. It tests matrix, cache and performance.

Transpose a matrix: return a matrix in which result[i][j] == matrix[j][i]. Then discuss how the matrix is laid out in memory and how to make the transpose fast for very large matrices.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • transpose(matrix: list[list[int]]) -> list[list[int]] returns a new n x m matrix for an m x n input.
  • transpose_in_place(matrix: list[list[int]]) -> None transposes a square matrix without extra storage.

Example

transpose([[1, 2, 3],
           [4, 5, 6]])     -> [[1, 4],
                               [2, 5],
                               [3, 6]]
transpose([[42]])          -> [[42]]
transpose([])              -> []

Constraints

  • Up to 200 x 200 for the coding part; values fit in 32 bits.
  • In place only for square matrices.

Clarifying questions

  • Is the input always square? If not, is an in-place answer still wanted (it is possible on a flat buffer, but much harder)?
  • Should the input be modified or left alone?
  • Is the matrix stored as nested lists or as one flat array in row-major order?

What a strong answer covers

  • The simple version in O(mn), and the square in-place version that swaps a[i][j] with a[j][i] only for j > i.
  • Row-major versus column-major storage: in a row-major array, reading by rows is sequential while writing the transpose jumps by a whole row each step, so the naive loop misses the cache on almost every write.
  • Blocking (tiling): transpose B x B tiles that fit in cache, so both reads and writes stay local; how to pick B from cache-line and cache sizes.
  • Very large matrices: parallelizing over tiles, and on a GPU loading tiles into shared memory so that global reads and writes are both coalesced, with padding to avoid bank conflicts.
  • In place for a non-square flat array: following the permutation cycles, and why it is rarely worth it.

Follow-ups

  • Measure the naive loop and the tiled one on a 4096 x 4096 matrix. What speedup do you expect, and why?
  • Write the GPU kernel for the tiled transpose.
  • When can you avoid the transpose entirely by changing how the next step indexes the data?

Where it's asked

  • Nvidia · Coding · Last asked Apr 2026Live coding, often C++-leaning; each round usually opens with 10 to 15 minutes on your past work · Assume no AI unless the interviewer offers it

What it tests

matrix · cache · performance

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding