Following questions have been asked in GATE CS 2014 exam.

**1) Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of the tree is of type treeNode.**

`typedef` `struct` `treeNode* treeptr; ` `struct` `treeNode { ` ` ` `treeptr leftMostChild, rightSibling; ` `}; ` `int` `DoSomething (treeptr tree) { ` ` ` `int` `value = 0; ` ` ` `if` `(tree != NULL) { ` ` ` `if` `(tree->leftMostChild == NULL) ` ` ` `value = 1; ` ` ` `else` ` ` `value = DoSomething(tree->leftMostChild); ` ` ` `value = value + DoSomething(tree->rightSibling); ` ` ` `} ` ` ` `return` `(value); ` `} ` |

*chevron_right*

*filter_none*

**When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned by the function corresponds to the**

(A) number of internal nodes in the tree.

(B) height of the tree.

(C) number of nodes without a right sibling in the tree.

(D) number of leaf nodes in the tree.

Answer: (D)

The important thing to note in this question is tree’s representation. Tree is represented as leftmost child and right sibling form. So if the leftmost child is NULL for a node, then there is no child of this node. If we take a look at the function, we can notice that the function increments the “value” by 1 only for a leaf node.

**2) Suppose a polynomial time algorithm is discovered that correctly computes the largest clique in a given graph. In this scenario, which one of the following represents the correct Venn diagram of the complexity classes P, NP and NP Complete (NPC)?**

Answer: (D)

Largest Clique is an NP complete problem. If one NP complete problem can be solved in polynomial time, then all of them can be. So NPC set becomes equals to P.

**Following are fill in the blanks questions**

**3)** The minimum number of comparisons required to find the minimum and the maximum of 100 numbers is _________________.

Answer: 147

The minimum number of comparisons required is 3n/2 – 3 for n numbers. Please refer method 3 of Maximum and minimum of an array using minimum number of comparisons for more details.

**4) **Consider two strings A = “qpqrr” and B = “pqprqrp”. Let x be the length of the longest common subsequence (not necessarily contiguous) between A and B and let y be the number of such longest common subsequences between A and B. Then x + 10y = ___.

Answer: 34

The longest length is 4. There are 3 LCS of length 4 “qprr”, “pqrr” and “qpqr”.

**5)** Suppose P, Q, R, S, T are sorted sequences having lengths 20, 24, 30, 35, 50 respectively. They are to be merged into a single sequence by merging together two sequences at a time. The number of comparisons that will be needed in the worst case by the optimal algorithm for doing this is ____.

Answer: 358

To merge two lists of size m and n, we need to do m+n-1 comparisons in worst case. Since we need to merge 2 at a time, the optimal strategy would be to take smallest size lists first. The reason for picking smallest two items is to carry minimum items for repetition in merging.

We first merge 20 and 24 and get a list of 44 using 43 worst case comparisons. Then we merge 30 and 35 into a list of 65 using 64 worst case comparisons. Then we merge 50 and 44 into a list of 94 using 93 comparisons. Finally we merge 94 and 65 using 158 comparisons. So total number of comparisons is 43 + 64 + 93 + 158 which is 358.

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above