What this CPU cache behaviour interview question tests
This is an easy computer architecture question that probes intuition about cache locality and memory access patterns. It asks you to reason about the performance difference between two functions operating on the same data, where one accesses elements in a predictable order and the other does not.
The question rewards understanding of how modern CPUs optimise for spatial locality—the tendency to access nearby memory addresses in quick succession. When the cache is cold, the function that scans memory sequentially will benefit from cache-line prefetching and fewer cache misses, while random access defeats these optimisations. For sufficiently large arrays, this difference dominates runtime.
- Cache lines and prefetching behaviour
- Sequential vs. random memory access patterns
- Cold-cache initialisation and warm-up effects