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
Coding & Algorithms
Software Engineer
NVIDIA
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
matrix
tiling
cache locality
memory coalescing
shared memory
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported

Related Questions