Explain Cache Misses in Matrix Transpose and Write a Blocked Version
Last updated: November 29, 2026
Quick Overview
A naive transpose of a large row-major matrix runs several times slower than a copy of the same size; explain why in terms of cache lines and TLB, then implement a tiled transpose and reason about the tile size.
0
0
0 solved
A naive transpose of a large row-major matrix runs several times slower than a copy of the same size; explain why in terms of cache lines and TLB, then implement a tiled transpose and reason about the tile size.
Performance-focused teams at Google (TPU compilers, image pipelines, Search serving) expect engineers to reason about memory hierarchy, not only big-O. The interviewer checks that you can connect access patterns to cache lines and propose a measurable fix.
What the Interviewer Expects
- Explain row-major layout and 64 byte cache lines
- Show why column-order writes touch a new line on every element
- Use tiling so each tile's rows and columns stay in cache
- Pick a tile size from cache size and element width
- Propose how to measure with perf counters
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Why do power-of-two matrix widths make this worse?
- How would you choose the tile size automatically?
- How would SIMD or a cache-oblivious recursive transpose compare?
- How does this relate to choosing NCHW versus NHWC tensor layouts?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
transpose_naive(a, n)andtranspose_blocked(a, n, b)operate on a flat row-major list ofn*nelements and return a new flat list.- Results must be identical.
- Explain the expected speedup in...
High-Level Design
In row-major order, a[i][j] and a[i][j+1] sit next to each other, so one 64 byte line holds 16 floats. The naive loop reads a along a row (good, one miss per 16 elements) but writes out[j][i] ...