Algorithms and Data Structures · Senior · Question 51 of 75
What is cache-friendly about iterating a 2D array row by row in C-like layouts?
- 1Rows are always shorter than the columns are
- ✓Correct answer: Rows are contiguous, so reads hit the same cache lines
- 3The CPU can predict row access but not column access
Explanation
With row-major layout a[i][j] and a[i][j+1] are neighbors. Iterating by columns jumps a whole row each step and misses the cache, which can be several times slower for the same O(n²) work.