Question 1 |

In a complete k-ary tree, every internal node has exactly k children. The number of leaves in such a tree with n internal nodes is: (GATE CS 2005)

nk | |

(n – 1) k+ 1 | |

n( k – 1) + 1 | |

n( k – 1) |

**Misc**

**Discuss it**

Question 1 Explanation:

For an k-ary tree where each node has k children or no children, following relation holds
L = (k-1)*n + 1
Where L is the number of leaf nodes and n is the number of internal nodes.
Let us see following for example

o / | \ o o o / | \ / | \ o o o o o o / | \ o o o k = 3 Number of internal nodes n = 4 Number of leaf nodes = (k-1)*n + 1 = (3-1)*4 + 1 = 9

Question 2 |

Given 8 identical coins out of which one coin is heavy and a pan balance. How many minimum number of measurements are needed to find the heavy coin?

2 | |

3 | |

4 | |

7 |

**Misc**

**Discuss it**

Question 2 Explanation:

Divide the coins into three groups and name the coins according to there group: A: A1, A2, A3 B: B1, B2, B3 C: C1, C2 Measure group A and group B. Two cases arise: 1. They are equal. One more measurement is needed to find the heavy coin in group C. Total two measurements needed in this case. 2. They are not equal. Find the heavy group, say A. Pick any two coins from this group, say A1 and A3. Measure A1 and A3 in the pan balance. Two cases arise: 2.1 They are equal. A2 is the heavy coin. Total two measurements needed. 2.2 They are not equal. It is known which of A1 or A3 is heavy. Total two measurements needed. So, the above observations says that in any case, 2 measurements are enough to find the heavy coin.Follow up:Generalize the minimum number of measurements forncoins with one coin heavy.

Question 3 |

In a village, people build houses in the same side of the road. A thief plans to loot the village. He wants maximum amount of money without having any risk of getting caught. By some means, the villagers know that their adjacent house is being looted or not and thus they become alert. So the thief cannot loot contiguous two houses. Given that the thief knows the amount of money stored in each house and the road is straight and there is no turning, which is the most efficient algorithmic strategy to solve this problem?

Brute-force | |

Dynamic Programming | |

Backtracking | |

Divide and Conquer |

**Misc**

**Discuss it**

Question 3 Explanation:

If we take a closer look, the problem boils down to: Given an array with some finite size where each element represents a positive number, find the maximum sum such that no two elements are adjacent. Dynamic Programming is the efficient technique to solve this. The algorithm can be given as follows: Maintain an auxiliary array loot.loot[0] = arr[0] loot[1] = arr[1] loot[i] = max(loot[i - 1], loot[i - 2] + arr[i]),2 <= i < nloot[n - 1] gives the maximum amount of money the thief can take away.

Question 4 |

Which of the following is not an in-place algorithm?

Insertion sort | |

Selection sort | |

Merge sort | |

Heap sort |

**Misc**

**Discuss it**

Question 4 Explanation:

An in-place algorithm is an algorithm which uses a constant amount of extra space apart from input.
Merge sort uses an extra O(n) space in the merging part.

Question 5 |

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

Consider the following algorithm in which x,y and z are Boolean arrays of size n:

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=O; i<n; ++i) z[i] = (x[i] ^ ~y[i]) V (~x[i] ^ y[i]) }The set Z computed by the algorithm is:

(X Intersection Y) | |

(X Union Y) | |

(X-Y) Intersection (Y-X) | |

(X-Y) Union (Y-X) |

**Misc**

**Discuss it**

Question 5 Explanation:

The expression x[i] ^ ~y[i]) results the only 1s in x where corresponding entry in y is 0. An array with these set bits represents set X – Y
The expression ~x[i] ^ y[i]) results the only 1s in y where corresponding entry in x is 0. An array with these set bits represents set Y – X.
The operator “V” results in Union of the above two sets.

Question 6 |

The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is
(A) (n)
(B) (logn)
(C) (log*n)
(D) (n)

A | |

B | |

C | |

D |

**Misc**

**Discuss it**

Question 6 Explanation:

Please see the post http://geeksforgeeks.org/?p=4722 for details.

Question 7 |

An element in an array X is called a leader if it is greater than all elements to the right of it in X. The best algorithm to find all leaders in an array (GATE CS 2006)

Solves it in linear time using a left to right pass of the array | |

Solves it in linear time using a right to left pass of the array | |

Solves it using divide and conquer in time 8(nlogn) | |

Solves it in time 8(n2) |

**Misc**

**Discuss it**

Question 7 Explanation:

Please see this for explanation.

Question 8 |

Consider the following C function.

float f(float x, int y) { float p, s; int i; for (s=1, p=1, i=1; i < 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)

x^y | |

e^x | |

ln(1 + x) | |

x^x |

**Misc**

**Discuss it**

Question 8 Explanation:

The function f() is implementation of Taylor's Series to calculates e^x

e^x = 1 + x + x^2/2! + x^3/3! + ---More is the value of y more precise value of e^x will be returned by f()

Question 9 |

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

(GATE CS 2000)do, where 1 < k <= n: reverse (s, 1, k); reverse (s, k + 1, n); reverse (s, 1, n);

Rotates s left by k positions | |

Leaves s unchanged | |

Reverses all elements of s | |

None of the above |

**Misc**

**Discuss it**

Question 9 Explanation:

Effect of the above 3 reversals for any k is equivalent to left rotation of the array of size n by k. Please see this post for details.
If we rotate an array n times for k = 1 to n, we get the same array back.

Question 10 |

An inversion in a an array A[] is a pair (A[i], A[j]) such that A[i] > A[j] and i < j. An array will have maximum number of inversions if it is:

Sorted in increasing order | |

Sorted in decreasing order | |

Sorted in alternate fashion | |

Both A and B |

**Misc**

**Discuss it**

Question 10 Explanation:

If the array is sorted in decreasing order, each pair will be an inversion.
e.g. 5, 4, 3, 2, 1
Maximum number of inversions possible = n * (n - 1) / 2 where n is the size of the array.

Question 11 |

The procedure given below is required to find and replace certain characters inside an input character string supplied in array A. The characters to be replaced are supplied in array oldc, while their respective replacement characters are supplied in array newc. Array A has a fixed length of five characters, while arrays oldc and newc contain three characters each. However, the procedure is flawed

void find_and_replace(char *A, char *oldc, char *newc) { for (int i = 0; i < 5; i++) for (int j = 0; j < 3; j++) if (A[i] == oldc[j]) A[i] = newc[j]; }The procedure is tested with the following four test cases (1) oldc = "abc", newc = "dab" (2) oldc = "cde", newc = "bcd" (3) oldc = "bca", newc = "cda" (4) oldc = "abc", newc = "bac" The tester now tests the program on all input strings of length five consisting of characters ‘a’, ‘b’, ‘c’, ‘d’ and ‘e’ with duplicates allowed. If the tester carries out this testing with the four test cases given above, how many test cases will be able to capture the flaw?

Only one | |

Only two | |

Only three | |

All four |

**Misc**

**Discuss it**

Question 11 Explanation:

The test cases 3 and 4 are the only cases that capture the flaw. The code doesn't work properly when an old character is replaced by a new character and the new character is again replaced by another new character. This doesn't happen in test cases (1) and (2), it happens only in cases (3) and (4).

Question 12 |

In the above question, if array A is made to hold the string “abcde”, which of the above four test cases will be successful in exposing the flaw in this procedure?

None | |

2 only | |

3 and 4 only | |

4 only |

**Misc**

**Discuss it**

Question 12 Explanation:

1
#include
#include
void find_and_replace(char *A, char *oldc, char *newc) {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 3; j++)
if (A[i] == oldc[j]) A[i] = newc[j];
}
int main()
{
char *oldc1 = "abc", *newc1 = "dab";
char *oldc2 = "cde", *newc2 = "bcd";
char *oldc3 = "bca", *newc3 = "cda";
char *oldc4 = "abc", *newc4 = "bac";
char test[] = "abcde";
printf("Test 2\n");
printf("%s\n", test);
find_and_replace(test, oldc2, newc2);
printf ("%s\n", test);
printf("\nTest 3\n");
strcpy(test, "abcde");
printf("%s\n", test);
find_and_replace(test, oldc3, newc3);
printf ("%s\n", test);
printf("\nTest 4\n");
strcpy(test, "abcde");
printf("%s\n", test);
find_and_replace(test, oldc4, newc4);
printf ("%s\n", test);
}
[/sourcecode]
Output:

Test 2 abcde abbcd Test 3 abcde addde Test 4 abcde aacde

Question 13 |

There are 25 horses among which you need to find out the fastest 3 horses. You can conduct race among at most 5 to find out their relative speed. At no point you can find out the actual speed of the horse in a race. Find out how many races are required to get the top 3 horses.

5 | |

7 | |

8 | |

9 |

**Misc**

**Discuss it**

Question 13 Explanation:

Divide the horses in 5 groups and run 5 races. Take toppers of 5 races and run 6th race more race, the topper of this race will be the fastest horse among all 25. Now run the 7th race among following horses.
1) Second and third fastest horses of 6th race
1) Take the second and third fastest horses of the group which belong to the topper of 6th race.
2) Take the second fastest horse of the group which belongs to second fastest horse of 6th race.
The fastest and second fastest of 7th race are the 2nd and 3rd fastest among all 25.

Question 14 |

Which of the following is the best possible time complexity to get Nth Fibonacci number with O(1) extra space

Time complexity T(n) is T(n-1) + T(n-2) which is exponential | |

O(n) | |

O(Logn) | |

O(n^2) |

**Misc**

**Discuss it**

Question 14 Explanation:

The best possible time complexity is O(Logn). See Program for nth Fibonacci number

Question 15 |

You are given an array with even integer elements. You and some other player take turns to pick numbers. Each player can pick either the leftmost element or the rightmost number. Find the maximum possible score (sum of numbers chosen) by you.
For example: if array is 5 8 4 2, then player you can choose either 5 or 2. Suppose you choose 2, then the other player can choose 5 or 4. Irrespective of what player 2 chooses, in next round you will have chance to choose 8. Thus, maximum possible score by player you, in this scenario, is (8+2)=10.
This problem can efficiently solved using?

Greedy Algorithm | |

Dynamic Programming | |

Backtracking | |

Recursion. |

**Misc**

**Discuss it**

Question 15 Explanation:

Question 16 |

What is the return value of following function for 484? What does it to in general?

bool fun(int n) { int sum = 0; for (int odd = 1; n > sum; odd = odd+2) sum = sum + odd; return (n == sum); }

False, it checks whether a given number is power of 3 | |

False, it checks whether a given number is even or not | |

False, it checks whether a given number is odd or not | |

True, it checks whether a given number is perfect square. |

**Misc**

**Discuss it**

Question 16 Explanation:

The given function adds all odd numbers 1, 3, 5, 7, 9, 11.... till the sum is smaller than n. If the sum becomes equal to n, then it returns true. This is basically a test for perfect square numbers.
All perfect square numbers can be written as sum of odd numbers.
4 = 1 + 3
9 = 1 + 3 + 5
16 = 1 + 3 + 5 + 7
36 = 1 + 3 + 5 + 7 + 9
49 = 1 + 3 + 5 + 7 + 9 + 11

Question 17 |

Consider the following C function in which size is the number of elements in the array E:
The value returned by the function MyX is the

int MyX(int *E, unsigned int size) { int Y = 0; int Z; int i, j, k; for(i = 0; i < size; i++) Y = Y + E[i]; for(i = 0; i < size; i++) for(j = i; j < size; j++) { Z = 0; for(k = i; k <= j; k++) Z = Z + E[k]; if (Z > Y) Y = Z; } return Y; }

maximum possible sum of elements in any sub-array of array E. | |

maximum element in any sub-array of array E. | |

sum of the maximum elements in all possible sub-arrays of array E | |

the sum of all the elements in the array E. |

**Misc**

**GATE-CS-2014-(Set-1)**

**Discuss it**

Question 17 Explanation:

**Explanation:**The function does following Y is used to store maximum sum seen so far and Z is used to store current sum 1) Initialize Y as sum of all elements 2) For every element, calculate sum of all subarrays starting with arr[i]. Store the current sum in Z. If Z is greater than Y, then update Y.

Question 18 |

Consider the expression tree shown. Each leaf represents a numerical value, which can either be 0 or 1. Over all possible choices of the values at the leaves, the maximum possible value of the expression represented by the tree is ___.

4 | |

6 | |

8 | |

10 |

**Misc**

**GATE-CS-2014-(Set-2)**

**Discuss it**

Question 18 Explanation:

An Expression Tree is a binary tree in which each internal node corresponds to operator and each leaf node corresponds to operand so for example expression tree for 3 + ((5+9)*2) would be:.
Below diagram shows values to pick to get the maximum value in expresison tree

Question 19 |

The minimum number of arithmetic operations required to evaluate the polynomial P(X) = X

^{5}+ 4X^{3}+ 6X + 5 for a given value of X using only one temporary variable.6 | |

7 | |

8 | |

9 |

**Misc**

**GATE-CS-2014-(Set-3)**

**Discuss it**

Question 19 Explanation:

P(X) = xPlease comment below if you find anything wrong in the above post.^{5}+ 4x^{3}+ 6x + 5 =x ( x^{4}+ 4x^{2}+ 6 ) +5 =x ( x ( x^{3}+ 4x ) + 6 ) + 5 =x ( x ( x ( x^{2}+ 4 ) ) + 6 ) + 5 =x ( x ( x (x (x) + 4 ) ) + 6 ) + 5 Let T be a temporary variable to store intermediate results. 1. T = (x) * (x) 2. T = T + 4 3. T = (x) * (T) 4. T = (x) * (T) 5. T = T + 6 6. T = (x) * T 7. T = T + 5 Thus, we need 7 operations if we are to use only one temporary variable.

Question 20 |

Suppose there are ⌈ log n ⌉ sorted lists of ⌊ n/log n ⌋ elements each. The time complexity of producing a sorted list of all these elements is :
(Hint : Use a heap data structure)

O(n log log n) | |

θ(n log n) | |

Ω(n log n) | |

Ω(n3/2) |

**Misc**

**GATE-CS-2005**

**Discuss it**

Question 20 Explanation:

We can merge x arrays of each size y in in O(xy*Logy) time using Min Heap.
x = n/Logn
y = Logn
We get O(n/Logn * Logn * Log Log n) which is O(nLogLogn)

Question 21 |

We are given 9 tasks T1, T2.... T9. The execution of each task requires one unit of time. We can execute one task at a time. Each task Ti has a profit Pi and a deadline di Profit Pi is earned if the task is completed before the end of the dith unit of time.

Task T1 T2 T3 T4 T5 T6 T7 T8 T9 Profit 15 20 30 18 18 10 23 16 25 Deadline 7 2 5 3 4 5 2 7 3Are all tasks completed in the schedule that gives maximum profit?

All tasks are completed | |

T1 and T6 are left out | |

T1 and T8 are left out | |

T4 and T6 are left out |

**Misc**

**GATE-CS-2005**

**Discuss it**

Question 21 Explanation:

Task T1 T2 T3 T4 T5 T6 T7 T8 T9 Profit 15 20 30 18 18 10 23 16 25 Deadline 7 2 5 3 4 5 2 7 3To maximize profit, we can finish tasks in following order T7, T2, T9, T5, T3, T8, T1

Question 22 |

We are given 9 tasks T1, T2.... T9. The execution of each task requires one unit of time. We can execute one task at a time. Each task Ti has a profit Pi and a deadline di Profit Pi is earned if the task is completed before the end of the dith unit of time.

Task T1 T2 T3 T4 T5 T6 T7 T8 T9 Profit 15 20 30 18 18 10 23 16 25 Deadline 7 2 5 3 4 5 2 7 3What is the maximum profit earned?

147 | |

165 | |

167 | |

175 |

**Misc**

**GATE-CS-2005**

**Discuss it**

Question 22 Explanation:

Task T1 T2 T3 T4 T5 T6 T7 T8 T9 Profit 15 20 30 18 18 10 23 16 25 Deadline 7 2 5 3 4 5 2 7 3To maximize profit, we can finish tasks in following order T7, T2, T9, T5, T3, T8, T1. We get the maximum profit as 23 + 20 + 25 + 18 + 30 + 16 + 15 = 147

Question 23 |

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = D1D2…Dm

int n, rev; rev = 0; while (n > 0) { rev = rev*10 + n%10; n = n/10; }The loop invariant condition at the end of the ith iteration is:

n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1 | |

n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1 | |

n != rev | |

n = D1D2….Dm and rev = DmDm-1…D2D1 |

**Misc**

**GATE-CS-2004**

**Discuss it**

Question 23 Explanation:

The loop one by adds digits to rev starting from the last digit of n. It also removes digits from n starting from left.

Question 24 |

Consider the following C program

main() { int x, y, m, n; scanf ("%d %d", &x, &y); /* Assume x > 0 and y > 0 */ m = x; n = y; while (m! = n) { if (m > n) m = m - n; else n = n - m; } print f ("% d", n); }The program computes

x ÷ y using repeated subtraction | |

x mod y using repeated subtraction | |

the greatest common divisor of x and y | |

the least common multiple of x and y |

**Misc**

**GATE-CS-2004**

**Discuss it**

Question 24 Explanation:

The given program is iterative implementation of Euclid's Algorithm for GCD

Question 25 |

What does the following algorithm approximate?

x = m; y = 1; while (x - y > e) { x = (x + y)/2; y = m/x; } print(x);(Assume m > 1, e > 0).

log m | |

m ^{2} | |

m ^{1/2} | |

m ^{1/3} |

**Misc**

**GATE-CS-2004**

**Discuss it**

Question 25 Explanation:

The given code is implementation of Babylonian method for square root

Question 26 |

In a permutation a1.....an of n distinct integers, an inversion is a pair (ai, aj) such that i < j and ai > aj. If all permutations are equally likely, what is the expected number of inversions in a randomly chosen permutation of 1.....n ?

n(n - 1)/2
| |

n(n - 1)/4 | |

n(n + 1)/4 | |

2n[log2 n] |

**Misc**

**GATE-CS-2003**

**Discuss it**

Question 26 Explanation:

There are n(n-1)/2 pairs such that i < j. For a pair (ai, aj), probability of being inversion is 1/2. Therefore expected value of inversions = 1/2 * (n(n-1)/2) = n(n-1)/4

Question 27 |

The cube root of a natural number n is defined as the largest natural number m such that m

^{3}≤ n. The complexity of computing the cube root of n (n is represented in binary notation) is:O(n) but not O(n ^{0.5}) | |

O(n ^{0.5}) but not O((log n)^{k}) for any constant k > 0 | |

O((log n) ^{k}) for some constant k > 0, but not O ((log log n)^{m}) for any constant m > 0 | |

O((log log n) ^{m}) for some constant k > 0.5, but not O((log log n)^{0.5}) |

**Misc**

**GATE-CS-2003**

**Discuss it**

Question 27 Explanation:

Question 28 |

The following are the starting and ending times of activities A, B, C, D, E, F, G and H respectively in chronological order: "a

_{s}b_{s}c_{s}a_{e}d_{s}c_{e}e_{s}f_{s}b_{e}d_{e}g_{s}e_{e}f_{e}h_{s}g_{e}h_{e}" Here, x_{s}denotes the starting time and x_{e}denotes the ending time of activity X. W need to schedule the activities in a set of rooms available to us. An activity can be scheduled in a room only if the room is reserved for the activity for its entire duration. What is the minimum number of rooms required ?3 | |

4 | |

5 | |

6 |

**Misc**

**GATE-CS-2003**

**Discuss it**

Question 28 Explanation:

Room1 - As
Room2 - Bs
Room3 - As
now A ends (Ae) and now Room3 is free
Room3-Ds
now A ends (Ae) and Room1 is free
Room1-Es
Room4-Fs
now B ends Room2 is free
now D ends Room3 is free
Room2-Gs
now E ends Room1 free
now F ends Room4 free
Room1-Hs
now G and H ends.
Totally used 4 rooms
Source: https://www.gatementor.com/viewtopic.php?f=267&t=2195

Question 29 |

In the following C program fragment, j, k n and TwoLog_n are interger variables, and A is an array of integers. The variable n is initialized to an integer ≥ 3, and TwoLog_n is initialized to the value of 2*⌈log2(n)⌉

for (k = 3; k < = n; k++) A[k] = 0; for (k = 2; k < = TwoLog_n; k++) for (j = k + 1; j < = n; j++) A[j] = A[j] || (j % k); for (j = 3; j < = n; j++) if (!A[j]) printf("%d", j);The set of numbers printed by this program fragment is

{m | m ≤ n, (∃ i) [m = i!]} Here i! mean factorial of i | |

{m | m ≤ n, (∃ i) [m = i ^{2}]} | |

{m | m ≤ n, m is prime} | |

{} |

**Misc**

**GATE-CS-2003**

**Discuss it**

Question 29 Explanation:

1
// Initialize all values as 0
for (k = 3; k < = n; k++)
A[k] = 0;
for (k = 2; k < = TwoLog_n; k++)
for (j = k + 1; j < = n; j++)
// If k divides j, then A[j] is
// set as 0, else non-zero
A[j] = A[j] || (j % k);
// Print all numbers where A[j] is 0
for (j = 3; j < = n; j++)
if (!A[j])
printf("%d", j);
[/sourcecode]

Question 30 |

An n x n array v is defined as follows:

v[i, j] = i-j for all i, j, 1 <= i <= n, 1 <= j <= nThe sum of the elements of the array v is

0 | |

n-1 | |

n ^{2} - 3n + 2 | |

n ^{2} (n+1)/2 |

**Misc**

**GATE-CS-2000**

**Discuss it**

Question 30 Explanation:

In this case, the matrix would be

Thus, A is the correct choice.

Please comment below if you find anything wrong in the above post.

0 -1 -2 -3 -4 -5 -6 -7 ... -n 1 0 -1 -2 -3 -4 -5 -6 ... -(n-1) 2 1 0 -1 -2 -3 -4 -5 ... -(n-2) 3 2 1 0 -1 -2 -3 -4 ... -(n-3) 4 3 2 1 0 -1 -2 -3 ... -(n-4) 5 4 3 2 1 0 -1 -2 ... -(n-5) 6 5 4 3 2 1 0 -1 ... -(n-6) 7 6 5 4 3 2 1 0 ... -(n-6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . n n-1 n-2 n-3 n-4 n-5 n-6 n-7 ... 2 1Now, we take the sum of first row and first column, which comes out to be zero. Similarly, we take the sum of second row and second column, third row and third column, and so on, and it is found that all have a sum equal to zero. So, the sum of all the elements in the matrix is zero.

Thus, A is the correct choice.

Please comment below if you find anything wrong in the above post.

Question 31 |

X, Y and Z are closed intervals of unit length on the real line. The overlap of X and Y is half a unit. The overlap of Y and Z is also half a unit. Let the overlap of X and Z be k units. Which of the following is true?

k must be 1 | |

k must be 0 | |

k can take any value between 0 and 1 (d) None
of the above | |

None of the above |

**Misc**

**GATE-CS-2000**

**Discuss it**

Question 32 |

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 <= k <= n:

reverse(s, 1, k) ; reverse(s, k + 1, n); reverse(s, l, n);

Rotates s left by k positions | |

Leaves s unchanged
| |

Reverses all elements of s | |

None of the above |

**Misc**

**GATE-CS-2000**

**Discuss it**

Question 32 Explanation:

Effect of the above 3 reversals for any k is equivalent to left rotation of the array of size n by k. Please see this post for details.
If we rotate an array n times for k = 1 to n, we get the same array back.

Question 33 |

Given an array that represents elements of arithmetic progression in order. It is also given that one element is missing in the progression, the worst case time complexity to find the missing element efficiently is:

Θ(n) | |

Θ(nLogn) | |

Θ(Logn) | |

Θ(1) |

**Misc**

**GATE-CS-2015 (Mock Test)**

**Discuss it**

Question 33 Explanation:

We can use Binary Search to find the missing element. See following link for details.
http://www.geeksforgeeks.org/find-missing-number-arithmetic-progression/

Question 34 |

Let swap() be a function that swaps two elements using their addresses. Consider the following C function.

void fun(int arr[], int n) { for (int i = 0; i < n; i+=2) { if (i>0 && arr[i-1] > arr[i] ) swap(&arr[i], &arr[i-1]); if (i<n-1 && arr[i] < arr[i+1] ) swap(&arr[i], &arr[i + 1]); } }If an array {10, 20, 30, 40, 50, 60, 70, 80} is passed to the function, the array is changed to

{20, 10, 40, 30, 60, 50, 80, 70}
| |

{10, 30, 20, 40, 60, 50, 80, 70} | |

{10, 20, 30, 40, 50, 60, 70, 80} | |

{80, 70, 60, 50, 40, 30, 20, 10} |

**Misc**

**GATE-CS-2015 (Mock Test)**

**Discuss it**

Question 34 Explanation:

The function sorts the array in waveform. See Sort an array in wave form for more details.

Question 35 |

Match the following

List-I A. Prim’s algorithm for minimum spanning tree B. Floyd-Warshall algorithm for all pairs shortest paths C. Mergesort D. Hamiltonian circuit List-II 1. Backtracking 2. Greed method 3. Dynamic programming 4. Divide and conquer Codes: A B C D (a) 3 2 4 1 (b) 1 2 4 3 (c) 2 3 4 1 (d) 2 1 3 4

a | |

b | |

c | |

d |

**Misc**

**GATE-CS-2015 (Set 1)**

**Discuss it**

Question 35 Explanation:

Prim's Algorithm is a greedy algorithm where greedy choice is to pick minimum weight edge from cut that divides already picked vertices and vertices yet to be picked.
Floyd-Warshall algorithm for all pairs shortest paths is a Dynamic Programming algorithm where we keep updating the distance matrix in bottom up manner.
Merge Sort is clearly divide and conquer which follows all steps of divide and conquer. It first divides the array in two halves, then conquer the two halves and finally combines the conquered results.
Hamiltonian circuit is a NP complete problem, that can be solved using Backtracking

Question 36 |

Consider the following pseudo code, where x and y are positive integers.

begin q := 0 r := x while r >= y do begin r := r – y q := q + 1 end endThe post condition that needs to be satisfied after the program terminates is

{r = qx + y ∧ r < y} | |

{x = qy + r ∧ r < y} | |

{y = qx + r ∧ 0 < r < y} | |

{ q + 1 < r–y ∧ y > 0} |

**Misc**

**GATE-CS-2015 (Set 1)**

**Discuss it**

Question 36 Explanation:

The given pseudo code does following for given x and y which positive integers.

1) It initializes r as x. 2) It repeatedly subtracts y from r until r becomes smaller than y. For every subtraction, it increments count q. 3) Finally r contains remainder, i.e., x%y and q contains ⌊x/y⌋See below pseudo code with comments.

begin q := 0 // q is going to contain floor(x/y) r := x // r is going to contain x % y // Repeatedly subtract y from x. while r >= y do begin r := r – y q := q + 1 end end

Question 37 |

Let a

_{n}represent the number of bit strings of length n containing two consecutive 1s. What is the recurrence relation for a_{n}?a _{n–2} + a_{n–1} + 2^{n–2} | |

a _{n–2} + 2a_{n–1} + 2^{n–2} | |

2a _{n–2} + a_{n–1} + 2^{n–2} | |

2a _{n–2} + 2a_{n–1} + 2^{n–2} |

**Misc**

**GATE-CS-2015 (Set 1)**

**Discuss it**

Question 37 Explanation:

**Simple Solution**One way to solve this is to try for small values and rule out options.

aIf we check for a_{0}= 0 a_{1}= 0 a_{2}= 1 ["11"] a_{3}= 3 ["011", "110", "111"] a_{4}= 8 ["0011", "0110", "0111", "1101", "1011", "1100", "1110", "1111"]

_{3}, we can see that only A and C satisfy the value. Among (A) and (C), only (A) satisfies for a

_{4}.

**Another Solution (With Proof)**

A string of length n (n >= 2) can be formed by following 4 prefixes 1)11followed by a string of length n-2 2)00followed by a string of length n-2 3)01followed by a string of length n-2 4)10followed by a string of length n-2 Number 1 has already two consecutive 1's so number of binary strings beginning with number 3 is 2^{n-2}as remaining n-2 bits can have any value. Number 2 has two 0's so remaining n-2 bits must have two consecutive 1's. Therefore number of binary strings that can be formed by number 2 is a_{n-2}. Number 3 and Number 4 together form all strings of length n-1 and two consecutive 1's.

Question 38 |

Given below are some algorithms, and some algorithm design paradigms.

List-I A. Dijkstra’s Shortest Path B. Floyd-Warshall algorithm to compute all pairs shortest path C. Binary search on a sorted array D. Backtracking search on a graph List-II 1. Divide and Conquer 2. Dynamic Programming 3. Greedy design 4. Depth-first search 5. Breadth-first searchMatch the above algorithms on the left to the corresponding design paradigm they follow Codes:

A B C D (a) 1 3 1 5 (b) 3 3 1 5 (c) 3 2 1 4 (d) 3 2 1 5

a | |

b | |

c | |

d |

**Misc**

**GATE-CS-2015 (Set 2)**

**Discuss it**

Question 38 Explanation:

Dijkstra’s Shortest Path is a Greedy Algorithm. Floyd-Warshall algorithm is Dynamic Programming. Binary search is a Divide and Conquer. Backtracking is Depth-first search

Question 39 |

Suppose c = 〈c[0], ... , c[k – 1]〉 is an array of length k, where all the entries are from the set {0, 1}. For any positive integers a and n, consider the following pseudocode.

DOSOMETHING (c, a, n) z ← 1 for i ← 0 to k – 1 do z ← zIf k = 4, c = 〈1, 0, 1, 1〉, a = 2 and n = 8, then the output of DOSOMETHING(c, a, n) is ____________.^{2}mod n if c[i] = 1 then z ← (z × a) mod n return z

0 | |

1 | |

2 | |

3 |

**Misc**

**GATE-CS-2015 (Set 3)**

**Discuss it**

Question 39 Explanation:

DOSOMETHING (c, a, n) z ← 1 for i ← 0 to k – 1 do z ← zIf k = 4, c = 〈1, 0, 1, 1〉, a = 2 and n = 8, then the output of DOSOMETHING(c, a, n) is ____________.^{2}mod n if c[i] = 1 then z ← (z × a) mod n return z

For i = 0, z = 1 mod 8 = 1, since c[0] = 1, z = 1*2 mod 8 = 2. For i = 1, z = 2*2 mod 8 = 1, since c[1] = 0, z remains 4. For i = 2, z = 16 mod 8 = 0Once z becomes 0, none of the statements inside DOSOMETHING() can make it non-zero.

Question 40 |

The following function computes X

^{Y}for positive integers X and Y.int exp(int X, int Y) { int res = 1, a = X, b = Y; while ( b != 0 ) { if ( b%2 == 0) { a = a*a; b = b/2; } else { res = res*a; b = b-1; } } return res; }Which one of the following conditions is TRUE before every iteration of the loop

A | |

B | |

C | |

D |

**Misc**

**GATE-CS-2016 (Set 2)**

**Discuss it**

Question 40 Explanation:

We can solve this question taking any two values for X and Y.
Suppose X= 2 and Y= 5, now this code will calculate
Looking at each iteration separately
Before iteration 1 – X=2 Y= 5 a=2 , b=5, res=1
Iteration 1 – since b%2 !=0 we go to else part
Therefore after iteration 1, X=2, Y=5, a=2, b=4, res=2
Iteration 2 – since b%2=0 we go to if part
Therefore after iteration 2 , X=2, Y=5, a=4, b=2, res=2
Iteration 3 – since b%2=0 we go to if part
Therefore after iteration 3 , X=2, Y=5, a=16, b=1, res=2
Iteration 4 – since b%2!=0 we go to else part
Therefore after iteration 4 , X=2, Y=5, a=16, b=0, res=32
Now putting the values of X, Y , a, b, res in the equations given in options after each
iteration we can see only equation c is correct.
This solution is contributed by

**Parul sharma.****Another solution**In option CBefore Iteration 1: X^Y=64 res ∗ (a^b)=64 Before Iteration 2: X^Y=64 res ∗ (a^b)=64 Before Iteration 3: X^Y=64 res ∗ (a^b)=64

There are 40 questions to complete.