ProgrammingVery hard

A list of 10^6 nodes is built by prepending, and then each node is looked up by value in a loop. What is the total cost of 10^6 lookups?

  1. A. About 5 x 10^11 node visits, which is too slow without an index
  2. B. About 10^6 node visits, since prepending keeps the list sorted
  3. C. About 10^12 visits only on a cyclic list
  4. D. About 20 visits per lookup, like a balanced tree

Correct answer: A. About 5 x 10^11 node visits, which is too slow without an index

Each lookup may scan the whole list, which is 10^6 steps, so 10^6 lookups cost about N^2 / 2. A hash map or sorted array is the fix.

More C Programming questions

Exams that ask this topic