What this CPU cache and memory-access interview question tests
This is an easy C++ question that probes whether you understand how memory locality and CPU caching affect runtime performance. Rather than asking you to optimize code from scratch, it asks you to reason about two existing implementations and predict which one the processor will execute faster.
To answer well, you need to recognize patterns like sequential vs. random memory access, cache line utilization, and branch prediction. These are not abstract concepts—they directly determine whether data sits in L1 cache (nanoseconds) or must be fetched from main memory (hundreds of nanoseconds). Interviewers use questions like this to check whether you think about real hardware constraints when you code, not just algorithmic complexity.
- Spatial and temporal locality
- Cache line size and alignment
- Prefetching and memory access patterns
- Branch prediction and instruction-level parallelism