Test 2 Algorithms | Computer Science

Description: GATE Previous year Topic Wise Questions and Answers | Algorithms
Number of Questions: 20
Created by:
Tags: Algorithms GATE CS
Attempted 0/20 Correct 0 Score 0

Let T be a depth first search tree in an undirected graph G. Vertices u and n are leaves of this tree T.

The degrees of both u and n in G are at least 2. Which one of the following statements is true?

  1. There must exist a vertex w adjacent to both u and n in G.

  2. There must exist a vertex w whose removal disconnects u and n in G.

  3. There must exist a cycle in G containing u and n.

  4. There must exist a cycle in G containing u and all its neighbours in G.


Correct Option: A
Explanation:

The median of n elements can be found in O(n) time. Which one of the following is correct about the complexity of quick sort, in which the median is selected as pivot?

  1. O(n)

  2. O(n log n)

  3. O(n)3

  4. O(n)2


Correct Option: B
Explanation:

If we use median as a pivot element, then the recurrence for all cases becomes T(n) = 2T(n/2) + O(n) The above recurrence can be solved using Master Method. It falls in case 2 of master method.

A set X can be represented by an array x [n] as follows:

x$\left [ i \right ]=\begin {cases} 1 & \text{if } i \in X \\ 0 & otherwise \end{cases}$ Consider the following algorithm in which x, y and z are boolean arrays of size n. algorithm zzz (x [ ], y[ ], z [ ] ) { int i; for (i = 0 ; i< n; ++i) z[i] = (x[i] Ù ~y[i]) Ú (~x[i] Ù y[i]) }

The set Z computed by the algorithm is

  1. (X U Y)

  2. (X $\cap$Y)

  3. (X − Y) $\cap$ (Y − X)

  4. (X − Y) U (Y − X)


Correct Option: D
Explanation:

$\text{Here}\ z = (x \land y') \lor (x' \land y)$

The order of the following recurrence: $T(n) = 2T([\sqrt{n}]) + 1$
is

  1. T (n) = $\Theta$(log log n)

  2. T (n) = $\Theta$ (log n)

  3. T (n) = $\Theta$ ($\sqrt{n}$)

  4. T (n) = $\Theta$ (n)


Correct Option: A
Explanation:

Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code.

S1: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell swap S2: On execution the code will generate a runtime error on line L1 S3: On execution the code will generate a runtime error on line L2 S4: The program will print 13 and 8 S5: The program will print 13 and - 2 Exactly the following set of statement(s) is correct:

  1. S1 and S2

  2. S1 and S4

  3. S3

  4. S1 and S5


Correct Option: B
Explanation:

Given two arrays of numbers a1,..........., an and b1,............, bn where each number is 0 or 1, the fastest algorithm to find the largest span (i, j ) such that ai + ai+1 + .........+ aj = bi + bi + 1 +...........+ bj or report that there is not such span,

  1. Takes $\Theta$(3n) and $\Omega$(2n) time if hashing is permitted

  2. Takes $\Theta$(n3) and $\Omega$ (n2.5) time in the key comparison model

  3. Takes $\Theta$(n3) time and space

  4. Takes $\Theta$($\sqrt{n}$) time only if the sum of the 2n elements is an even number


Correct Option: C
Explanation:

The maximum number of binary trees that can be formed with three unlabeled nodes is:

  1. 1

  2. 5

  3. 4

  4. 3


Correct Option: B
Explanation:

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:

  1. 2h - 1

  2. 2h-1 - 1

  3. 2h+1 - 1

  4. 2h+1


Correct Option: C
Explanation:

Which of the following sorting algorithms has the lowest worst-case complexity?

  1. Merge sort

  2. Bubble sort

  3. Quick sort

  4. Selection sort


Correct Option: A
Explanation:

The in order and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively The post order traversal of the binary tree is:

  1. d e b f g c a

  2. e d b g f c a

  3. e d b f g c a

  4. d e f g b c a


Correct Option: A
Explanation:

Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4) mod 7. 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 − denotes an empty location in the table.

  1. 8, −, −, −, −, −, 10

  2. 1, 8, 10, −, −, −, 3

  3. 1, −, −, −, −, −, 3

  4. 1, 10, 8, −, −, −, 3


Correct Option: B
Explanation:

In an unweighted, undirected connected graph, the shortest path from a node S to every other node is computed most efficiently, in terms of time complexity, by

  1. applying Dijkstra’s algorithm starting from S

  2. applying Warshall’s algorithm

  3. performing a DFS starting from S

  4. performing a BFS starting from S


Correct Option: D
Explanation:

A complete n-ary tree is a tree in which each node has n children or no children. Let I be the number of internal nodes and L be the number of leaves in a complete n-ary tree. If L = 41, and I = 10, what is the value of n?

  1. 3

  2. 4

  3. 5

  4. 6


Correct Option: C
Explanation:

What is the time complexity of the following recursive function?

Int Do Something (int n) {
return 1;
else
return (Do Something (floor sqrt (n))) + n);
  1. $\Theta$(n2)

  2. $\Theta$(n log2 n)

  3. $\Theta$(log2 n)

  4. $\Theta$(log2 log2 n)


Correct Option: D
Explanation:

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?

  1. There is a minimum spanning tree containing e.

  2. 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.

  3. Every minimum spanning tree has an edge of weight w.

  4. e is present in every minimum spanning tree.


Correct Option: D
Explanation:

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:

  1. $\Theta$ (log2 n)

  2. $\Theta$(log2 log2 n)

  3. $\Theta$ (n)

  4. $\Theta$(nlog2 n)


Correct Option: A
Explanation:

In the following C function, let n $\ge$ m.

Int gcd (n,m) { 
  if (n% m ==0) return m;
  n = n %m;
  return gcd (m, n);
}

How many recursive calls are made by this function?

  1. $\Theta$(log2 n)

  2. $\Omega$(n)

  3. $\Theta$(log2log2 n)

  4. $\Theta$($\sqrt n$)


Correct Option: A
Explanation:

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?

  1. At least 2n − c comparisons, for some constant c, are needed.

  2. At most 1.5n − 2 comparisons are needed.

  3. At least nlog2 comparisons are needed.

  4. None of the above.


Correct Option: B
Explanation:

Consider the following C code segment:

int Is Prime (n) 
{
int i, n;
for (i = 2; i &lt;= sqrt (n) ; i ++)
 if (n% i == 0) { 
   print f (“ Not Prime  n”);
   return 0; 
  } 
  return 1;
}

Let T (n) denote the number of times the for loop is executed by the program on input n. Which of the following is TRUE?

  1. T (n) = O($\sqrt n$) and T (n) =$\Omega$($\sqrt n$)

  2. T (n) = O($\sqrt n$) and T (n) = $\Omega$ (1)

  3. T (n) = O(n) and T (n) = $\Omega$ ($\sqrt n$)

  4. None of the above


Correct Option: B
Explanation:

Suppose the letters a, b, c, d, e, f have probabilities $ \dfrac{1}{2},\dfrac{1}{4},\dfrac{1}{8},\dfrac{1}{16},\dfrac{1}{32},\dfrac{1}{32}$ respectively.

Which of the following is the Huffman code for the letter a, b, c, d, e, f?

  1. 0, 10, 110, 1110, 11110, 11111

  2. 11, 10, 011, 010, 001, 000

  3. 11, 10, 01, 001, 0001, 0000

  4. 110, 100, 010, 000, 001, 111


Correct Option: A
Explanation:

- Hide questions