Algorithms and Data Structures · Middle · Question 70 of 75

Why are arrays usually faster to iterate than linked lists even though both are O(n)?

Explanation

Big O hides constants. Contiguous memory is prefetched in cache lines; list nodes scattered around the heap cause cache misses. That's why arrays win in practice for most workloads.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 70

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz