Skip to content
Official PYQ · 2004

GATE CS 2004 Q40 — Linked Lists

Explain with AI

Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations among union, intersection, membership, cardinality\text{union, intersection, membership, cardinality} will be the slowest?

Choose one option
View worked solution

Answer: D

Let the two sets AA and BB be represented as linked lists of sizes mm and nn respectively, with elements in arbitrary (unsorted) order.

Let's analyze the worst-case time complexity for each operation:

  1. Membership: To check if an element xx belongs to a set of size nn, we perform a linear search through the linked list.
    Time Complexity: O(n)O(n)
  2. Cardinality: To find the number of elements in a set of size nn, we traverse the entire list to count the nodes.
    Time Complexity: O(n)O(n)
  3. Intersection: To find A∩BA \cap B, we must check each of the mm elements of list AA against all nn elements of list BB to find common elements.
    Time Complexity: O(m⋅n)O(m \cdot n)
  4. Union: To find A∪BA \cup B without duplicates, we can copy all elements of AA to the result list. Then, for each of the nn elements of BB, we must search the list AA (or the result list) to ensure it is not a duplicate before adding it.
    Time Complexity: O(m⋅n)O(m \cdot n)

Comparing these, union and intersection require quadratic time O(n2)O(n^2) (assuming m≈nm \approx n), making them significantly slower than membership and cardinality, which only require linear time O(n)O(n).

Therefore, the slowest operations are union and intersection.

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

Published
Report a correction
Keep exploringMore Linked Lists questions