GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A priority queue is a sorted singly linked list (ascending). Insert scans from the head, comparing the new key with each node, and stops at the first node whose key is >= the new key (inserting before it), or at the end. Keys 15, 8, 23, 4, 16, 42, 8, 11 are inserted, then three extract-min operations are done. How many key comparisons are made by the insertions in all?

  1. A. 21
  2. B. 19
  3. C. 16
  4. D. 28

Correct answer: B. 19

Count each node examined: for every insertion this is the number of smaller nodes plus one stopping node (if any). The total is 19; extract-min needs none.

More Programming and Data Structures (GATE) questions

Exams that ask this topic