Test 4 - Algorithms | Computer Science(CS)

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

Randomized quick sort is an extension of quick sort where the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using randomized quick sort?

  1. 0(n)

  2. 0(nlog n)

  3. 0(n2)

  4. 0(n!)


Correct Option: B
Explanation:

In randomized quick sort pivot is chosen randomly, the case complexity of sorting n. In that case the worst case 0(n2) of quick sort become 0(nlog n) of randomize quick sort.

Consider any array representation of an n element binary heap where the elements are stored from index 1 to index n of the array. For the element stored at index i of the array $(i \leq n)$, the index of the parent is

  1. $i-1$

  2. $\lfloor \frac{i}{2} \rfloor$

  3. $\lceil \frac{i}{2} \rceil$

  4. $\frac{(i+1)}{2}$


Correct Option: B
Explanation:

Let f (n) = n2 logn and g(n) = n(logn)10 be two positive functions of n. Which of the following statements is correct?

  1. f (n) = 0(g(n)) and g(n) $\ne$Y 0(f (n))

  2. g (n) = 0(f (n)) and f (n) $\ne$Y 0(g(n))

  3. f (n) $\ne$Y 0(g(n)) and g(n) $\ne$Y 0(f (n))

  4. f (n) = 0(g(n)) and g(n) = 0(f (n))


Correct Option: A
Explanation:

The solution to the recurrence equation $T(2^k) = 3T(2^{k-1})+1, T(1) =1$ is

  1. $2^k$

  2. $\frac{(3^{k+1}-1)}{2}$

  3. $3^{\log_2 k}$

  4. $2^{\log_3 k}$


Correct Option: B
Explanation:

What is the minimum number of stacks of size n required to implement a queue to size n?

  1. One

  2. Two

  3. Three

  4. Four


Correct Option: B
Explanation:

To implement a queue of size n using stacks each of size n require minimum 2 stacks.

How many undirected graphs (not necessarily connected) can be constructed out of a given set $V={v_1, v_2, \dots v_n}$ of $n$ vertices?

  1. $\frac{n(n-1)} {2}$

  2. $2^n$

  3. $n!$

  4. $2^\frac{n(n-1)} {2} $


Correct Option: D
Explanation:

Given n vertices the various cases which are possible, no. connection, 1 connection it can be selected nc 2 ways, 2 connections & then (n − 1) connections to make complete graph $2^{^nC_2} $= $2^\frac{n(n-1)} {2} $

The minimum number of colours required to colour the vertices of a cycle with n nodes in such a way that no two adjacent nodes have same colour is.

  1. 2

  2. 3

  3. 4

  4. $n − 2 \left[ \dfrac{n}{2} \right] + 2$


Correct Option: B
Explanation:

In the worst case, the number of comparisons needed to search a single linked list of length n for a given element is

  1. $\log n$

  2. $\dfrac{n}{2}$

  3. $\log_2 n -1$

  4. $n$


Correct Option: D
Explanation:

Worst case of searching occurs when the element to be searched is at the end of the list so no. of comparisons required to search complete list would be n.

Maximum number of edges in a n-node undirected graph without self loops is

  1. n2

  2. $\dfrac{n(n-1)}{2}$

  3. (n-1)

  4. $\dfrac{(n-1)(2)}{2}$


Correct Option: B
Explanation:

The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is:

  1. $\frac{n}{2}$

  2. $\frac{(n-1)}{3}$

  3. $\frac{(n-1)}{2}$

  4. $\frac{(2n+1)}{3}$


Correct Option: D
Explanation:

To evaluate an expression without any embedded function calls

  1. One stack is enough

  2. Two stack are needed

  3. As many stacks as the height of the expression tree are needed

  4. A Turning machine is needed in the general case


Correct Option: A
Explanation:

To evaluate an expression we need only 1 stack in which the operands & operators are pushed into, & then evaluated using pop operations.

Consider the undirected unweighted graph G. Let a breadth-first traversal of G be done starting from a node r . Let d(r,u) and d(r,v) be the lengths of the shortest paths form r to u and v respectively in G. If u is visited before v during the breadth-first traversal, which of the following statements is correct?

  1. d(r,u) < d(r,v)

  2. d(r,u) > d(r,v)

  3. d(r,u) $\le$ d(r,v)

  4. None of the above


Correct Option: C
Explanation:

In BFS if u is visited before v then either u is some levels before v or u & v are at the same level but u is leftmost in v. d(r,u) $\le$ d(r,v)

The running time of the following algorithm Procedure A(n) If n <= 2 return (1) else return (A($|\sqrt n|$)); Is best described by

  1. 0(n)

  2. 0(log n)

  3. 0(loglog n)

  4. 0(1)


Correct Option: B
Explanation:

Level order traversal of a rooted tree can be done by stating from the root and performing

  1. preorder traversal

  2. inorder traversal

  3. depth first search

  4. breadth first search


Correct Option: D
Explanation:

Level order traversal is done by traversing all the vertices in a particular level & them moving to next level. This is some as breadth first search where level by level search is done.

A weight-balanced tree is a binary tree in which for each node, the number of nodes in the let sub tree is at least half and at most twice the number of nodes in the right sub tree. The maximum possible height (number of nodes on the path from the root to the furthest leaf) of such a tree on n nodes is best described by which of the following?

  1. $\log_2 n$

  2. $\log_{\frac{4}{3}} n$

  3. $\log_3 n$

  4. $\log_{\frac{3}{2}} n$


Correct Option: A
Explanation:

Consider the following algorithm for searching for a given number x in an unsorted array A[1.....n] having n distinct values : (1) Choose an i uniformly at random from 1....n If A[i] = x then stop else Goto 1;

Assuming that x is present A, What is the expected number of comparisons made by the algorithm before it terminates?

  1. n

  2. n -1

  3. 2n

  4. $\dfrac{n}{2}$


Correct Option: A
Explanation:

Given array A[1.....n], an element A[i] is chosen randomly from 1 to n.This would require n selections & comparisons to find x in array

Given the following input(4322,1334,1471,9679,1989,6171,6173,4199) and the hash function x mod 10, which of the following statements are true?

  1. 9679,1989,4199 hash to the same value
  2. 1471,6171 hash to the same value
  3. All element hashes to a different value
  1. 1 only

  2. 2 only

  3. 1 and 2 only

  4. 3 and 4 only


Correct Option: C
Explanation:

The tightest lower bound on the number of comparisons, in the worst case, for comparision-based sorting is of the order of

  1. n

  2. n2

  3. nlog n

  4. nlog2n


Correct Option: C
Explanation:

Sorting worst case occurs when arrays are reverse sorted, we need to select every element once & for every element min no. of comparison might be log2n. So overall min. complexity 0(nlog n)

Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely?

  1. preorder and postorder
  2. inorder and postorder
  3. preorder and inorder
  4. level order and postorder
  1. 1 only

  2. 2 and 3

  3. 3 only

  4. 4 only


Correct Option: B
Explanation:

For a tree we not only require in order & preorder but also post order traversal. Preorder & post order help to determine the roots of binary subtrees, inorder arranges those roots in order.

Two matrices M1 and M2 are to be stored in arrays A and B respectively. Each array can be stored either in row-major or column major order in contiguous memory locations. The time complexity of an algorithm to compute M1x M2 x will be

  1. best if A is in row-major, and B is in column-major order

  2. best if both are in row-major order

  3. best if both are in column-major order

  4. independent of the storage scheme


Correct Option: D
Explanation:

Since the matrices are stored in array, there is no dependence of time complexity on row major or column major. Here only the starting address is known & on the basis of indexes the next memory locations are calculated.

- Hide questions