This document contains a 30 question mid-semester exam for a data structures and algorithms course. The exam covers topics like asymptotic analysis, sorting algorithms, hashing, binary search trees, and recursion. It provides multiple choice questions to test understanding of algorithm time complexities, worst-case inputs, and recursive functions. Students are instructed to attempt all questions in the 2 hour time limit and notify the proctor if any electronic devices other than calculators are used.
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Ee693 sept2014midsem
1. EE693 DATA STRUCTURES AND ALGORITHMS
MID-SEMESTER EXAMINATION
22 SEPTEMBER, 2014, Time: 2 Hours
EEE Department, IIT Guwahati
NOTE: Attempt and solve all the questions. Question-1 to Question-30 are multiple choice questions (more than one
choices may be correct). Question-1 to Question-30, marks will be awarded, if and only if, all the correct
choice(s) will be made. Use of any kind of electronic media other than calculators are strictly prohibited. If
anybody finds voiding this rule will be penalized with -10 marks penalty. Please do not forget to mention your
Name and Roll No in the paper sheet. Please tick the correct choice(s). All the questions are of 1
mark.
Name: Roll No:
1. This is the welcome question! All the best for EE693 Mid-Semester Examination.
Out of all the 2-digit integers between 1 and 100, a 2-digit number has to be selected at random. What is the
probability that the selected number is not divisible by 7?
(a) 13/90 (b) 12/90 (c) 78/90 (d) 77/90
2. Consider the following function
int unknown( int n)
{
int i , j , k = 0;
for ( i=n /2; i<=n ; i++)
for ( j =2; j<=n ; j=j ∗2)
k=k+n /2;
return k ;
}
The return value of the function is
(a) Θ(n2
) (b) Θ(n2
log n) (c) Θ(n3
) (d) Θ(n3
log n)
3. Let g(n) and f(n) denote respectively, the worst case and average case running time of an algorithm executed on an
input of size n. Which of the following is ALWAYS TRUE?
(a) f(n) = Ω(g(n)) (b) f(n) = Θ(g(n))
(c) f(n) = O(g(n)) (d) None of the above
4. Which of the given options provides the increasing order of asymptotic complexity of functions f1(n) = 2n
, f2(n) = n3/2
,
f3(n) = nlog2 n, f4(n) = nlog2 n
?
(a) f3,f2,f4,f1 (b) f3,f2,f1,f4 (c) f2,f3,f1,f4 (d) f2,f3,f4,f1
5. Two alternative packages A and B are available for processing a database having 10k
records. Package A requires
0.0001n2
time units and package B requires 10nlog10 n time units to process n records. What is the smallest value of
k for which package B will be preferred over A?
(a) 12 (b) 10 (c) 6 (d) 5
6. A list of n strings, each of length n, is sorted into lexicographic (dictionary) order using the merge-sort algorithm by
a machine that can compare alphabets. The worst case running time of this computation is
(a) O(nlog n) (b) O(n2
log n) (c) O(nlog2
n) (d) O(n2
)
7. Consider the Quicksort algorithm. Suppose there is a procedure for finding a pivot element which splits the list into
two sub-lists each of which contains at least one-fifth of the elements. Let T(n) be the number of comparisons required
to sort n elements. Then
(a) T(n) ≤ 2T(n/5) + n (b) T(n) ≤ T(n/5) + T(4n/5) + n
(c) T(n) ≤ 2T(4n/5) + n (d) T(n) ≤ 2T(n/2) + n
8. 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
(a) 2h
− 1 (b) 2h−1
− 1 (c) 2h+1
− 1 (d) 2h+1
2. 9. Given the selection sort function
selection_sort ( int s [ ] , int n)
{
int i , j ;
int min ;
for ( i =0; i<n ; i ++){
min=i ;
for ( j=i +1; j<n ; j++)
i f ( s [ j ] < s [ min ] ) min=j ;
swap(&s [ i ] ,& s [ min ] ) ;
}
}
the maximum number of swaps it might have to do is
(a) O(n2
/log n) (b) O(nlog n) (c) O(n) (d) O(log n)
10. Given a set of n distinct numbers, the fastest algorithm that finds
√
n numbers that are closest to the median has
the asymptotic performance
(a) O(
√
nlog n) (b) O(n) (c) O(nlog n) (d) O(n
√
n)
11. Let A be a sequence of 8 distinct integers sorted in ascending order. How many distinct pairs of sequences, B and
C are there such that (i) each is sorted in ascending order, (ii) B has 5 and C has 3 elements, and (iii) the result of
merging B and C gives A?
(a) 2 (b) 30 (c) 56 (d) 256
12. Find the worst inputs for Counting Sort among the following:
(a) A =< 3,5,3,2,2,4,1,6,4,2,4,1 >
(b) A =< 4,1,2,3,5,1,3,6,4,4,1,1 >
(c) A =< 3,5,3,2,2,2,1,6,2,2,2,1 >
(d) A =< 3,3,3,2,2,4,1,1,4,2,4,1 >
13. Find the worst inputs for Insertion Sort among the following (a to z):
(a) A =< a,d,o,b,e,s >
(b) A =< g,l,i,d,e,r >
(c) A =< n,o,m,a,d,s >
(d) A =< z,a,b,i,s,m >
14. Find the worst inputs for Merge Sort among the following (a to z):
(a) A =< a,d,o,b,e,s >
(b) A =< g,l,i,d,e,r >
(c) A =< n,o,m,a,d,s >
(d) A =< z,a,b,i,s,m >
15. Find the worst inputs for Quick Sort (if pivoting is done using the median at every step) among the following:
(a) A =< 3,5,1,6,2,4 >
(b) A =< 6,4,5,3,1,2 >
(c) A =< 3,1,4,5,6,2 >
(d) A =< 6,4,2,5,3,1 >
16. Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming sim-
ple uniform hashing, what is the expected number of collisions? Given {{k,l} k ≠ l and h(k) = h(l)}.
(a) n(n+1)
2m
(b) n(n+1)
m
(c) n(n−1)
2m
(d) n(n−1)
m
17. Consider a version of the division method in which h(k) = k mod m, where m = 2p
− 1 and k is a charac-
ter string interpreted in radix 2p
. If we can derive string x from string y by permuting its characters, then
(a) x and y hash to the same value.
(b) x and y hash to the different values.
(c) x and y hash values cannot be defined.
(d) None of the above
18. Suppose that we use an open-addressed hash table of size m to store n ≤ m/2 items. Assuming uniform hash-
ing, for i = 1,2,3,⋯,n, the probability is at most ⋯⋯ that the i insertion requires strictly more than k probes.
(a) 2k+1
(b) 2−k−1
(c) 2−k
(d) 2−k+1
3. 19. Suppose that we use an open-addressed hash table of size m to store n ≤ m/2 items. Assuming uniform hash-
ing, for i = 1,2,3,⋯,n, the probability is at most ⋯⋯ that the i insertion requires more than 2log(n) probes.
(a) 1/n (b) n (c) 1/(m + n) (d) 1/n2
20. Suppose that we have numbers between 1 and 1000 in a binary search tree, and we want to search
for the number 363. Which of the following sequences could not be the sequence of nodes examined?
(a) 2, 252, 401, 398, 330, 344, 397, 363
(b) 924, 220, 911, 244, 898, 258, 362, 363
(c) 925, 202, 911, 240, 912, 245, 363
(d) 2, 399, 387, 219, 266, 382, 381, 278, 363
(e) 935, 278, 347, 621, 299, 392, 358, 363
21. If a node in a binary search tree has two children, then its successor has no right child and its predecessor has no left
child.
(a) The statement is true. (b) The statement is false. (c) Cannot say. (d) None of the above.
22. We can sort a given set of n numbers by first building a binary search tree containing these numbers (using TREE-
INSERT repeatedly to insert the numbers one by one) and then printing the numbers by an inorder tree walk.
(a) The worst-case running times for this sorting algorithm is Θ(n2
).
(b) The best-case running times for this sorting algorithm is Θ(n log(n)).
(c) The worst-case running times for this sorting algorithm is Θ(log(n)).
(d) The best-case running times for this sorting algorithm is Θ(n).
23. Suppose a circular queue of capacity (n − 1) elements is implemented with an array of n elements. Assume
that the insertion and deletion operation are carried out using REAR and FRONT as array index variables,
respectively. Initially, REAR = FRONT = 0. The conditions to detect queue full and queue empty are
(a) Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT
(b) Full: (REAR+1) mod n == FRONT, empty: (FRONT+1) mod n == REAR
(c) Full: REAR == FRONT, empty: (REAR+1) mod n == FRONT
(d) Full: (FRONT+1) mod n == REAR, empty: REAR == FRONT
24. We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how
many ways can we populate the tree with the given set so that it becomes a binary search tree?
(a) 0 (b) 1 (c) n! (d) 1
n+1
2n
Cn
25. Consider the following C program that attempts to locate an element x in an array Y [] using binary search. The
program is erroneous.
1. f(int Y [10], int x) {
2. int i,j,k;
3. i = 0;j = 9;
4. do {
5. k = (i + j)/2;
6. if( Y [k] < x) i = k; else j = k;
7. } while((Y [k] ≠ x) && (i < j));
8. if(Y [k] == x) printf ("x is in the array ") ;
9. else printf (" x is not in the array ") ;
10. }
On which of the following contents of Y and x does the program fail?
(a) Y is [1 2 3 4 5 6 7 8 9 10] and x < 10
(b) Y is [1 3 5 7 9 11 13 15 17 19] and x < 1
(c) Y is [2 2 2 2 2 2 2 2 2 2] and x > 2
(d) Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even
26. In Question 25, the correction needed in the program to make it work properly is
(a) Change line 6 to: if (Y [k] < x) i = k + 1; else j = k − 1;
(b) Change line 6 to: if (Y [k] < x) i = k − 1; else j = k + 1;
(c) Change line 6 to: if (Y [k] ≤ x) i = k; else j = k;
(d) Change line 7 to: }while((Y [k] == x) && (i < j));
4. 27. Consider the following C program segment where CellNode represents a node in a binary tree:
struct CellNode {
struct CellNOde *leftChild;
int element;
struct CellNode *rightChild;
};
int GetValue (struct CellNode *ptr) {
int value = 0;
if (ptr != NULL) {
if ((ptr->leftChild == NULL) && (ptr->rightChild == NULL))
value = 1;
else
value = value + GetValue(ptr->leftChild)+ GetValue(ptr->rightChild);
}
return(value);
}
The value returned by GetValue when a pointer to the root of a binary tree is passed as its argument is:
(a) the number of nodes in the tree
(b) the number of internal nodes in the tree
(c) the number of leaf nodes in the tree
(d) the height of the tree
28. 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?
(a) At least 2n − c comparisons, for some constant c, are needed.
(b) At most 1.5n − 2 comparisons are needed.
(c) At least nlog2n comparisons are needed.
(d) None of the above.
29. What is the time complexity of the following recursive function:
int DoSomething (int n) {
if (n ≤ 2)
return 1;
else
return (DoSomething (floor(sqrt(n))) + n);
}
(a) Θ(n2
) (b) Θ(nlog2n) (c) Θ(log2n) (d) Θ(log2log2n)
30. In the following C function, let n geqm.
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?
(a) Θ(log2n) (b) Ω(n) (c) Θ(log2log2n) (d) Θ(
√
n)
All the best for your remaining mid-semester examinations.