Skip to content
Official PYQ · 2025

GATE CS 2025 Set 2 Q28 — Arrays, Stacks and Queues

Explain with AI

​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 nn of these data structures?

Choose one option
View worked solution

Answer: A

To find the worst-case time complexity for the meld operation on each of the given data structures of size nn:

P. Unsorted Doubly Linked List (with Head and Tail Pointers)

To meld two unsorted doubly linked lists, L1L_1 and L2L_2:

  • Connect the tail node of L1L_1 to the head node of L2L_2.
  • Update the tail pointer of the combined list to point to the tail of L2L_2.

Since we have direct pointers to the head and tail of both lists, this operation requires only a constant number of pointer updates, which takes Θ(1)\Theta(1) time.

Q. Min-heap (implemented using an array)

To meld two min-heaps of size nn represented as arrays:

  • Concatenate the two arrays of size nn into a single array of size 2n2n. This takes Θ(n)\Theta(n) time.
  • Apply the bottom-up heap construction algorithm (Build-Heap) on the combined array of size 2n2n.

The time complexity of Build-Heap on an array of size NN is linear, i.e., Θ(N)\Theta(N). Here, N=2nN = 2n, so the heapification takes Θ(2n)=Θ(n)\Theta(2n) = \Theta(n) time. Thus, the overall worst-case time complexity is Θ(n)\Theta(n).

R. Binary Search Tree (BST)

To meld two BSTs of size nn optimally:

  1. Perform an in-order traversal of both BSTs to produce two sorted arrays (or lists) of size nn. This takes Θ(n)\Theta(n) time.
  2. Merge the two sorted arrays of size nn into a single sorted array of size 2n2n. This takes Θ(n)\Theta(n) time.
  3. Reconstruct a balanced BST from the merged sorted array. This can be done in Θ(n)\Theta(n) time.

Thus, the overall worst-case time complexity to meld two BSTs is Θ(n)\Theta(n).

Comparing the results:

  • P:Θ(1)\mathrm{P}: \Theta(1)
  • Q:Θ(n)\mathrm{Q}: \Theta(n)
  • R:Θ(n)\mathrm{R}: \Theta(n)

This corresponds to Option A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions