Interview prompt
Explain why memory locality matters to an engineer who understands the surrounding system but has not used this technique. Walk from its contract to a concrete operation, then discuss where it fails or becomes expensive.
A strong answer
Processors use a hierarchy of storage because small, nearby storage can be accessed faster than large, distant storage. A cache keeps copies of recently or predictably used memory blocks. Programs benefit when accesses show temporal locality, reusing data, or spatial locality, touching nearby addresses.
A row-major matrix traversal that increments the inner column index typically consumes adjacent elements, while stepping through a column may jump by an entire row stride. The values are mathematically identical, but the second access pattern may fetch cache lines that contain mostly unused data.
A complete answer also calls out the assumptions that control correctness. A cache hit avoids a slower level; a miss fetches a block and may evict another useful block. Capacity, associativity, and line size shape conflict behavior. Caches improve typical access time but make performance depend on working-set size and access order.
Close by describing one representative test or measurement. Rewrite a matrix transpose loop so reads are contiguous. Then reason about the writes: why can optimizing one side still leave the other side with poor locality?
Follow-up questions
Answer the follow-ups in the frontmatter. Use the linked article for the concept and the trace to make the explanation concrete.