Data Structures and Algorithms | Set 12


Following questions have been asked in GATE CS 2007 exam.

1. Consider the following C program segment where CellNode represents a node in a binary tree:

struct CellNode 
  struct CellNOde *leftChild;
  int element;
  struct CellNode *rightChild;

int GetValue(struct CellNode *ptr) 
  int value = 0;
  if (ptr != NULL) 
   if ((ptr->leftChild == NULL) &&
        (ptr->rightChild == NULL))
      value = 1;
      value = value + GetValue(ptr->leftChild)
                   + GetValue(ptr->rightChild);

The value returned by GetValue() when a pointer to the root of a binary tree is passed as its argument is:
(A) the number of nodes in the tree
(B) the number of internal nodes in the tree
(C) the number of leaf nodes in the tree
(D) the height of the tree

Answer (C)
For explanation, please see our post for counting leaf nodes.

2. Consider the process of inserting an element into a Max Heap, where the Max Heap is represented by an array. Suppose we perform a binary search on the path from the new leaf to the root to find the position for the newly inserted element, the number of comparisons performed is:
(A) Θ(logn)
(B) Θ(LogLogn )
(C) Θ(n)
(D) Θ(nLogn)

Answer (B)
The height of a Max Heap is Θ(logn). If we perform binary search for finding the correct position then we need to do Θ(LogLogn) comparisons.

3. Let w be the minimum weight among all edge weights in an undirected connected graph. Let e be a specific edge of weight w . Which of the following is FALSE?
(A) There is a minimum spanning tree containing e.
(B) If e is not in a minimum spanning tree T, then in the cycle formed by adding e to T, all edges have the same weight.
(C) Every minimum spanning tree has an edge of weight w .
(D) e is present in every minimum spanning tree.

Answer (D)
(A), (B) and (C) are correct.
(D) is incorrect as there may be many edges of wight w in the graph and e may not be picked up in some of the minimum spanning trees.

4. An array of n numbers is given, where n is an even number. The maximum as well as the minimum of these n numbers needs to be determined. Which of the following is TRUE about the number of comparisons needed?
(A) At least 2n – c comparisons, for some constant c, are needed.
(B) At most 1.5n – 2 comparisons are needed.
(C) At least nLog2n comparisons are needed.
(D) None of the above.

Answer (B)

Please see the post for details.

5. Consider the following C code segment:

int IsPrime(n)
  int i,n;
   if(n%i == 0)
     {printf(“Not Prime\n”); return 0;}
  return 1;

Let T(n) denotes the number of times the for loop is executed by the program on input n. Which of the following is TRUE?
(A) T(n) = O(sqrt(n)) and T(n) = Ω(sqrt(n))
(B) T(n) = O(sqrt(n)) and T(n) = Ω(1)
(C) T(n) = O(n) and T(n) = Ω(sqrt(n))
(D) None of the above

Answer (B)
Big O notation describes the upper bound and Big Omega notation describes the lower bound for an algorithm.

The for loop in the question is run maximum sqrt(n) times and minimum 1 time. Therefore, T(n) = O(sqrt(n)) and T(n) = Ω(1)

Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:

1 Average Difficulty : 1/5.0
Based on 1 vote(s)

Writing code in comment? Please use, generate link and share the link here.