ProgrammingModerate
Which operation is O(1) in a C array, but O(n) in a singly linked list when the head is given?
- A. Insertion at the head of a list
- B. Access to the i-th element by index
- C. Removal of the head node of a list
- D. Reading the head pointer's value
Correct answer: B. Access to the i-th element by index
Arrays compute the address of element i directly, while a list must follow i links. Head insertion and removal touch only the head.
More C Programming questions
- A program reads a number n and then loops for (i = 0; i < n; i++) with n = 10^9 and a constant body. Roughly how long does it take in C…
- Which operation makes a sorted array of n elements support lookup in O(log n) and insertion in O(n)?
- For a char buffer of length 10^6 that is processed once, which loop is correct and fast enough?
- A matrix of 3000 x 3000 cells is read once and each cell is visited a constant number of times. Which complexity bound applies to the…
- A recursive merge sort on 10^6 elements makes how many comparisons, roughly?
- Which function call costs O(n) time per call and can be avoided by caching its result in a loop?