Misc

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)
A
nk
B
(n – 1) k+ 1
C
n( k – 1) + 1
D
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?
A
2
B
3
C
4
D
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 for n coins 
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?
A
Brute-force
B
Dynamic Programming
C
Backtracking
D
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 < n
loot[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?
A
Insertion sort
B
Selection sort
C
Merge sort
D
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:
gate_2006_50 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:
A
(X Intersection Y)
B
(X Union Y)
C
(X-Y) Intersection (Y-X)
D
(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) \theta(n) (B) \theta(logn) (C) \theta(log*n) (D) \theta(n)
A
A
B
B
C
C
D
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)
A
Solves it in linear time using a left to right pass of the array
B
Solves it in linear time using a right to left pass of the array
C
Solves it using divide and conquer in time 8(nlogn)
D
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)
A
x^y
B
e^x
C
ln(1 + x)
D
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
do, where 1 < k <= n:
  reverse (s, 1, k);
  reverse (s, k + 1, n);
  reverse (s, 1, n);
(GATE CS 2000)
A
Rotates s left by k positions
B
Leaves s unchanged
C
Reverses all elements of s
D
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:
A
Sorted in increasing order
B
Sorted in decreasing order
C
Sorted in alternate fashion
D
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.
There are 40 questions to complete.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.