Tiled Matrix Transpose for Cache-Friendly Memory Access
Last updated: November 29, 2026
Quick Overview
Write a function that transposes an m by n matrix by processing it in square tiles of a given size, and explain why tiling improves memory behavior compared with a naive row-by-column loop.
NVIDIA
November 29, 20260
0
0 solved
Write a function that transposes an m by n matrix by processing it in square tiles of a given size, and explain why tiling improves memory behavior compared with a naive row-by-column loop.
A standard early question at NVIDIA because the same idea appears in the classic CUDA shared-memory transpose: read a tile with coalesced loads, write it transposed with coalesced stores. The interviewer wants correct boundary handling for non-multiple sizes and a crisp explanation of locality.
What the Interviewer Expects
- Allocates an n by m output and maps out[j][i] = a[i][j]
- Loops over tiles and clamps tile bounds with min() for ragged edges
- Explains that naive transpose strides through one of the two arrays
- Relates tiling to caches on CPUs and shared memory on GPUs
- Mentions bank conflicts and the padding trick in the CUDA version
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you transpose a square matrix in place?
- In the CUDA shared memory version, why do people declare the tile as [32][33]?
- How would you choose the tile size on a CPU?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: a rectangular list of lists a (m rows, n columns) and tile. Output: the n x m transpose as a new matrix. Handle empty input and dimensions that are not multiples of the tile size....
High-Level Design
A naive double loop reads a row by row but writes out column by column, so every write lands on a different row, which on a CPU means a different cache line, and on a GPU means uncoalesced global ...