Data Structures & Algorithms · Interview question

Two nested loops summing a matrix have the same Big-O but different runtimes. Why?

A strong answer

Cache locality. C stores 2D arrays in row-major order, row 0's elements, then row 1's, contiguously. The CPU loads memory in cache lines (say 32 bytes) around each access. Traversing in row-major order (for r { for c { m[r][c] }}) walks consecutive addresses, so each loaded line is fully used before moving on, cache-friendly. Traversing column-major (for c { for r { m[r][c] }}) jumps COLS elements between accesses, touching a different cache line almost every time and evicting lines before their other bytes are used, cache-hostile. Both are O(n·m) in Big-O, which ignores the memory hierarchy, but on a cached core the row-major version can be several times faster. The lesson: match traversal order to storage order. Even on a cacheless Cortex-M0, sequential access helps flash prefetch and wait-states.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Arrays & Memory Layout

Contiguous storage is why arrays give O(1) indexing and why row-major traversal is cache-friendly, plus array-of-structs vs struct-of-arrays for embedded data.

More Arrays & Memory Layout questions

Browse all 472 interview questions
Two nested loops summing a matrix have the same Big-O but different runtimes. Why? | EmbeddedPrep.io