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?
- A. About 5 x 10^11 node visits, which is too slow without an index
- B. About 10^6 node visits, since prepending keeps the list sorted
- C. About 10^12 visits only on a cyclic list
- 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
- int p = malloc(10 sizeof p); free(p); free(p); What is the result?
- In C, which of these are true about malloc's return value (assume the request succeeds)?
- A program allocates an int array of size n with malloc(n sizeof(int)). For n = 2^31, on a platform with 32-bit sizet, what can go wrong?
- A malloc'd block of 16 bytes is written past its end by 4 bytes. What is the correct description?
- Which function call allocates storage for an array of 100 doubles, all set to 0.0, in one step?
- A function gets a pointer to a linked list and must keep the list unchanged but needs to walk it twice. Which kind of pointer parameter is…