Data Structures & AlgorithmsHard

Two sorted lists 1 4 6 9 and 2 3 7 8 10 are merged with the standard two-pointer merge (take the smaller head; on a tie take from the first list). Count the element comparisons made until one list is exhausted; the rest of the other list is then copied without comparisons.

  1. A. 9
  2. B. 8
  3. C. 4
  4. D. 20

Correct answer: B. 8

Outputs 1, 2, 3, 4, 6, 7, 8, 9 each need one comparison; then the first list is empty and 10 is copied free: 8. 9 = n + m counts the copy too, 4 is the best case min(n, m), and 20 compares every pair.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic