ProgrammingVery hard
A list of records must be kept sorted while new records arrive one at a time. Which structure gives the best total cost for 10^5 inserts and 10^5 searches?
- A. A linked list with a linear search per insertion, about 10^10 steps
- B. A full re-sort after every insert, about 10^10 steps
- C. A balanced binary search tree, with O(log n) insert and search
- D. An unsorted array with a full scan per search, about 10^10 steps
Correct answer: C. A balanced binary search tree, with O(log n) insert and search
A balanced tree gives O(log n) for both operations. The other options cost about 10^10 steps at this size.
More C Programming questions
- Which sort is stable and runs in O(n log n) worst case, suitable for struct arrays that must keep equal-key order?
- How many records remain after a struct array of 7 records is filtered to drop those with a key equal to the previous key, when the array…
- Rotating the 2 x 2 matrix [[1, 2], [3, 4]] by 90 degrees counter-clockwise gives which first row?
- The matrix int a[3][3] = {[1][1] = 5}; is zero-initialised everywhere else (C99 designators). What is a[0][0] + a[1][1] + a[2][2]?
- For the array int a[2][3] = {{1, 2, 3}, {4, 5, 6}}, what does ((a + 1) + 2) print?
- int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (r)[3] = m + 1; What is r[0][2]?