Open In App

Data Structures and Algorithms | Set 12

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

C




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;
   else
      value = value + GetValue(ptr->leftChild)
                   + GetValue(ptr->rightChild);
  }
  return(value);
}


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 https://www.geeksforgeeks.org/?p=2755 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 weight 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 https://www.geeksforgeeks.org/?p=4583 for details.

5. Consider the following C code segment:  

C




int IsPrime(n)
{
  int i,n;
  for(i=2;i<=sqrt(n);i++)
   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.
 



Previous Article
Next Article

Similar Reads

Data Structures and Algorithms | Set 3
Following questions have asked in GATE CS exam. 1. Suppose you are given an array s[1...n] and a procedure reverse (s,i,j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence do, where 1 &lt; k &lt;= n: reverse (s, 1, k); reverse (s, k + 1, n); reverse (s, 1, n); (GATE CS 2000) (a)
4 min read
Data Structures and Algorithms | Set 2
Following questions have been asked in GATE CS exam. 1. Consider the function f defined below. struct item { int data; struct item * next; }; int f(struct item *p) { return ( (p == NULL) || (p-&gt;next == NULL) || (( P-&gt;data &amp;lt;= p-&gt;next-&gt;data) &amp;amp;&amp;amp; f(p-&gt;next)) ); } For a given linked list p, the function f returns 1
5 min read
Data Structures and Algorithms | Set 4
Following questions have been asked in GATE CS exam. 1. Consider the following C program segment C/C++ Code struct CellNode { struct CelINode *leftchild; int element; struct CelINode *rightChild; } int Dosomething(struct CelINode *ptr) { int value = 0; if (ptr != NULL) { if (ptr-&gt;leftChild != NULL) value = 1 + DoSomething(ptr-&gt;leftChild); if
3 min read
Data Structures and Algorithms | Set 5
Following questions have been asked in GATE CS exam. 1. Consider the following C function. float f(float x, int y) { float p, s; int i; for (s=1, p=1, i=1; i &lt; y; i ++) { p*= x/i; s+=p; } return s; } For large values of y, the return value of the function f best approximates (GATE CS 2003) a) x^y b) e^x c) ln(1 + x) d) x^x Answer (b) The functio
2 min read
Data Structures and Algorithms | Set 6
Following questions have been asked in GATE CS exam. 1. The usual &Theta;(n^2) implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the position, the worst case running time will (GATE
3 min read
Data Structures and Algorithms | Set 7
Following questions have been asked in GATE CS 2006 exam. 1. In a binary max heap containing n numbers, the smallest element can be found in time (GATE CS 2006) (A) 0(n) (B) O(logn) (C) 0(loglogn) (D) 0(1) Answer (A) In a max heap, the smallest element is always present at a leaf node. So we need to check for all leaf nodes for the minimum value. W
3 min read
Data Structures and Algorithms | Set 8
Following questions have been asked in GATE CS exam. 1. Consider the following functions Which of the following is true? (GATE CS 2000) (a) h(n) is 0(f(n)) (b) h(n) is 0(g(n)) (c) g(n) is not 0(f(n)) (d) f(n) is 0(g(n)) Answer (d) g(n) = 2&radic;n Log n = n&radic;n f(n) and g(n) are of same asymptotic order and following statements are true. f(n) =
3 min read
Data Structures and Algorithms | Set 9
Follow questions have been asked in GATE CS exam. 1 In a heap with n elements with the smallest element at the root, the 7th smallest element can be found in time (GATE CS 2003) a) &Theta;(n log n) b) &Theta;(n) c) &Theta;(log n) d) &Theta;(1) Answer(d) The 7th smallest element must be in first 7 levels. Total number of nodes in any Binary Heap in
2 min read
Data Structures and Algorithms | Set 10
Following questions have been asked in GATE CS 2007 exam. 1. The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is: (A) 2^h -1 (B) 2^(h-1) - 1 (C) 2^(h+1) -1 (D) 2*(h+1) Answer (C) Maximum number of nodes will be there for a complete tree. Number of nodes in
4 min read
Data Structures and Algorithms | Set 11
Following questions have been asked in GATE CS 2007 exam. 1. Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4)mod7. Assuming the hash table is initially empty, which of the following is the contents of the table when the sequence 1, 3, 8, 10 is inserted into the table using closed hashing? Note that '_' den
4 min read