A meld operation on two instances of a data structure combines them into one…

2025

A meld operation on two instances of a data structure combines them into one single instance of the same data structure. Consider the following data structures:

P: Unsorted doubly linked list with pointers to the head node and tail node of the list.

Q: Min-heap implemented using an array.

R: Binary Search Tree.

Which ONE of the following options gives the worst-case time complexities for meld operation on instances of size 𝑛 of these data structures?

  1. A.

    P: Θ(1), Q: Θ(𝑛), R: Θ(𝑛)

  2. B.

    P: Θ(1), Q: Θ(𝑛 log 𝑛), R: Θ(𝑛)

  3. C.

    P: Θ(𝑛), Q: Θ(𝑛 log 𝑛), R: Θ(𝑛2)

  4. D.

    P: Θ(1), Q: Θ(𝑛), R: Θ(𝑛 log 𝑛)

Attempted by 240 students.

Show answer & explanation

Correct answer: A

Final answer: P: Θ(1), Q: Θ(n), R: Θ(n).

  • P (unsorted doubly linked list with head and tail pointers): Meld by linking the tail of the first list to the head of the second and updating head/tail pointers (and handling empty lists). This is a constant number of pointer updates, so Θ(1).

  • Q (min-heap implemented using an array): Concatenate the two underlying arrays into one array of size 2n and run build-heap (heapify) on the combined array. Heapify runs in linear time in the array size, so the cost is Θ(2n)=Θ(n). (Note: inserting elements one-by-one would be Θ(n log n), but that is not the optimal meld algorithm.)

  • R (binary search tree): Perform inorder traversal of each tree to produce two sorted lists (Θ(n)), merge the two sorted lists into one sorted list (Θ(n)), and build a BST from the merged sorted list (Θ(n)). Totals to Θ(n). (A naive approach inserting nodes one-by-one into an unbalanced tree could be Θ(n^2), but the optimal melding procedure is linear.)

Therefore the worst-case time complexities for optimal meld procedures are:

  1. Unsorted doubly linked list with head/tail pointers: Θ(1)

  2. Min-heap in array: Θ(n)

  3. Binary search tree: Θ(n)

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir