0% found this document useful (0 votes)
25 views

Data Structures and Algorithms

Uploaded by

Abhishek Abhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Data Structures and Algorithms

Uploaded by

Abhishek Abhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structures and

1) Let G be a graph with n vertices and m Algorithms


edges. What is the tightest upper
bound on the running time on Depth First Search of G?
is represented using Assume that the graph
matrix. adjacency
(A) O(n)
(B) O(m+n)
(C) O(n)
(D) O(mn)

2) Consider a rooted Binary tree represented using pointers. The best


bound on the time required to determine the number of subtrees havingupper
having exactly 4 nodes O(n" Logn "). Then the value of
is a+ 10b
3) Consider the directed graph given below. Which one of the following is
TRUE?

(A) The graph doesn't have any topological ordering


(B) Both PQRS and SRPQ are topological ordering
(C) Both PSRQ and SPRQ are topological ordering
(D) PSRQ is the only topological ordering
4) Let P be a QuickSort Program to sort numbers in ascending order using the
first element as pivot. Let t1 and t2 be the number of
for the inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, comparisons made by P
2} respectively. Which one of the
following holds?
(A) t1 =5
(B) t1 < t2
(C) t1> t2
(D) t1 = 12

5) 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; it+)
Y =
Y+ E[i];
for (i =
0; i <
size; it+)
for (j =
i; j <
size; j++)
Z = 0;
for (k =
i; k < j; k++)
=
Z + E[K];
if
if (Z >
Y)Zi
return Y;

(A) maximum possible sum of elements in any


sub-array of array E.
(B) maximum element in any sub-array of array E.
(C) sum of the maximum elements in all possible sub-arrays of array E
(D) the sum of all the elements in the array E.

1) What is the return value of f(p, p) if the value of p is initialized to 5 before the
call? Note that the first parameter is
passed by reference, whereas the second
parameter is passed by value.
int f(int &x, int c) {
C C-1;
if (c ==
0) return 1;
x = X + 1;
return f(x, C) * X;

(A) 3024
(B) 6561
(C) 55440
(D) 161051

1) The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25,
23, 39, 35, 42. Which one of the following is the postorder traversal sequence
of the same tree?
(A) 10, 20, 15, 23, 25, 35, 42, 39, 3O
(B) 15, 10, 25, 23, 20, 42, 35, 39, 30
(C) 15, 20, 10, 23, 25, 42, 35, 39, 30
(D) 15, 10, 23, 25, 20, 35, 42, 39, 30
3) Consider the following function
int unknown (int n).{
int i, j, k 0;
for (i n/2; i <= n; it+)
for (j 2; j e n; j = j * 2)
k = k + n/2;

2
return k;

What is the returned value of the above


function?
(A) O(n^2)
(B) O(n^2Logn)
(C) O(n^3)
(D) O(n^3Logn)
4) The number of elements that can be sorted in O(logn) time using heap sort
is
(A) O(1)
(B) O(sqrt(logn))
(C) O(Log n/(Log Log n))
(d) o(Log n)

5) 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; 1++)
for (int j = 0; j 3 j++)

if (Ali]=oldc[jl) A[il = newc[jl:

The procedure is tested with the following four test cases


(1) oldc "abc", nevwc ="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?
(A) Only one
(B) Only two
(C) Only three
(D) All four
6) 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?
(A) None
(B) 2 only
(C) 3 and 4 only
(D) 4 only

3
1) Which of the following statements islare TRUE for an undirected graph?
P: Number of odd
degree vertices is even
Q: Sum of degrees of all vertices is even
A) P Only
B) Q Only
C) Both P and Q
D) Neither P nor Q

2) Consider an undirected random graph of eight vertices. The probability that


there is an edge between a pair of vertices is 1/2. What is the
of unordered cycles of length three?
expected number
(A) 1 8
(B) 1
(C) 7
(D) 8

3) What is the time complexity of Bellman-Ford single-source shortest path


algorithm on a complete graph of n vertices?
(A) O(n)
(B) O(n Logn)
(C) e(n)
(D) O(n* Logn)
Answer (C).
Time complexity of Belliman-Ford algorithm is O(VE) where V is number of vertices
andE is number edges (See this). If the graph is complete, the value of E becomes
ov). So overall time complexity becomes o(Vv

4) Which of the following statements are TRUE?


(1) The problem of determining whether there exists a cycle in an undirected
graph is in P.
(2) The problem of determining whether there exists a cycle in an undirected
graph is in NP.
(3) If a problem A is NP-Complete, there exists a non-deterministic polynomial
time algorithm to solve A.
(A) 1,2 and 3
(B) 1 and 2 only
(C) 2 and 3 only
(D) 1 and3 only

5) Which one of the following is the tightest upper bound that represents the
time complexity of inserting an object into a binary search tree of n nodes?
(A) O(1)
(B) O(log n)
(C) O(n)
(D) O(n log n)

4
the
that represents
Which of the following is the tightest upper bound
6) one
selection sort?
number of swaps required to sort n numbers using
(A) O(logn)
(B) O(n)
(C) O(n log n)
(D) O(n2)
along with Enqueue and Dequeue
7) Consider the following operation
operations on

queues,where k is a global parameter


MultiDequeue (Q) {
m = k
and m 0) {
while (Q is not empty
Dequeue (Q)
m m 1

of n MultiDequeue)
time complexity of a sequence
What is the worst case

operations on an initially empty queue?


(A) On)
(B) On+ k)
(C) O(nk)
(D) (n) time of the Tower of Hanoi
relation capturing the optimal
1) The recurrence

discs is
problem with n

(A) T(n) 2T(n


=
-2) +2
(B) T(n) 2T(n -1)
= +n
+1
(C) T(n) 2T(n/2)
=

(D) T(n) =2T(n -1) +1 shown in the figure


below. There are multiple
Consider the directed graph
2) one will be reported by
vertices S and T. Which
shortest paths between Assume that, in any iteration,
the shortest

Dijstra?s shortest path algorithm? to v is


only when a strictly shorter path
path to a vertex v is updated
discovered.

5
2
1 G
A

4
3
D
3
5

B
(A) SDT
(B) SBDT
(C) SACDT
(D) SACET
3) Suppose circular queue of capacity
a
(n -1) elements is implemented with
an array of elements. Assume that the insertion
n
and deletion
carried out using REAR and FRONT as
array index
operation are
Initially, REAR =
FRONT 0. The conditions to detect
=
variables, respectively.
empty are queue full and queue
(A) Full: (REAR+1) mod n = = FRONT, empty: REAR FRONT ==

(B) Full: (REAR+1) mod n = = FRONT, empty:


(C) Full: REAR = = FRONT, empty: (REAR+1) (FRONT+1)
mod n ==
REAR
mod n = = FRONT
(D) Full: (FRONT+1) mod n = = REAR, empty: REAR == FRONT
1) Let w(n) and A(n) denote respectively, the worst case and
average
running time of an algorithm executed on an input of size n. which of case
the
following is ALWAYS TRUE?
(A) A(n) = Q(W(n))
(B) A(n) = O(W(n))
(C) A(n) = O(W(n))
(D) A(n) = o(W(n)

2) The worst case running time to search for an element in a balanced in a


binary search tree with n2An elements is
(A) O(n log n)
(B) O(n2")
(C) e(n)
(D) Olog n)
3) Assuming P != NP, which of the following is true ?
(A) NP-complete NP =

(B) NP-complete n P=o


(C) NP-hard = NP
(D) P =NP-complete

6
4) The height of a
the tree.
tree is
defined as the number of
The function shown
in the
edges on the longest path in
(root) to compute the height of a pseudocode below is invoked as
height
int height (treeptr n) binary tree rooted at the tree
pointer root.
{if (n== NULL) return -1;
if (n>
left= NULL)
if (n > right =NULL) return 0;
else return BI;
//Box 1
else {h1 height (n left);
=

if(n right= NULL) return (1+ h1);


else {h2 =height (n
>right);
return B2 //Box 2

The appropriate expression for the two boxes B1 and B2 are


(A) B1:(1 +height(n->right)), B2: (1 + max(h1,h2))
(B) B1 (height(n->right), B2: (1 +max(h1,h2))
(C) B1: height(n->right), B2: max(h1,h2)
(D) B1:(1 +height(n->right), B2: max(h1,h2)

5) A list of n string, each of length n, is sorted into lexicographic order


the merge-sort algorithm. The worst case using
running time of this computation is
(A) O(n log n)
(B) O(n log n)
(C) O(n^2 + log n)
(D) O(n^2)
1) An undirected graph G(V, E) contains n (n> 2) nodes named v1, v2 ,..vn.
Two nodes vi , vj are connected if and only if 0 < li j| <= 2. Each
-
is assigned a weight i +j. A sample graph with n = 4 is shown below.
edge (vi, vj)

What will be the cost of the minimum spanning tree (MST) of such a graph with
n nodes?
(A) 1/12(11n^2 - 5n))

(B) n^2-n +1
(C) 6n -11
(D)2n+1
2) The length of the path from v5 to v6 in the MST of
10 is previous question with n =

(A) 11
(B) 25
(C) 31
(D) 41
3) Consider two binary operators 't 'and ' ' with the
being lower than that of thet precedence of operator
operator. Operator is right associative while
operatori is left associative. Which one of the following represents the parse
treefor expression (7i 3t 4t31 2)?
(A) (B)

(C) (D)

1) A max-heap is a heap where the value of each parent is greater than or


equal to the values of its children. Which of the following is a max-heap?

(A) (B)

(C) (D)

8
Oary tree is max-heap if it is
i y ree in
TOaes are as which every level,
a
complete binary tree (A
far left as except possibly the complete binary tree is a
and it follows the last, completely filled, and all
is
parent is greater possible)
than or equal to
the values of its max-heap property (value of each
A) IS not a children).
5) IS a max-heap because it is not a
C) IS notmax-heap because it is complete complete binary tree
because 8 is a chilebinary
a
max-heap tree and follows
max-heap
property. of 5 in this
tree, so violates the property
D) nota
is max-heap
max-heap because 8 is a chile of 5 in this
property. There are many other nodes in tree, so violates the
this tree. this tree which violate max-heap
max-heap property in
2) Four matrices M1, M2, M3
and M4 of
respectively can be dimensions pxq, qxr, rxs and sxt
scalar multiplied is several ways with different
multiplications. For number of total
M4)), the total number of example, when multiplied as (M1 X M2) x (M3 X
(M1 X M2) X M3) X M4), themultiplications is pqr + rst +
total number of scalar prt. When multiplied as
+pst.
If p
multiplications
is + pqr prs
=10, q= 100, r
=20, s = 5 and
t =80, then the number of scalar
multiplications needed is
A) 24800o
B) 44000
C) 19000
D) 25000

3) Whichof the given


options
complexity of functions f1, f2, provides
the increasing order of
f3 and f4? asymptotic
f1(n) = 2^n
f2(n) = n^(3/2)
f3(n) =nLogn
f4(n) =n^(Logn)
A) f3, f2, f4, f1
B) 13, f2, f1, f4
C) f2, f3, f1, f4
D) f2, 13, f4, f1

4) We are given a set of n distinct elements and an unlabeled binary tree withn
nodes. In how many ways can we
populate the tree with the gfven set so that it
becomes a binary search tree?
A) 0
B) 1
C) n!
D) (1/(n+1)).2nCn
5) An algorithm to find the length of the longest monotonically increasing
sequence of numbers in an array A[0 :n-1] is given below.
Let Li denote the length of the longest monotonically increasing sequence
starting at index i in the array

9
Initialize Ln-1 =1
For all i such that 0
sisn -2
ifa []<a [i+1]
Otherwise

Finally the length of the longest monotonically


Max (L,Lr ns). increasing sequence is

Which of the following statements is TRUE?


(A) The algorithm uses dynamic
programming paradigm
(B) The algorithm has a linear complexity and uses branch and bound
(C) The algorithm has a non-linear polynomial paradiqgm
bound paradigm complexity and uses branch and
(D) The algorithm uses divide and conquer
1 Considera
complete undirected graph paradigm.
with vertex set {0, 1, 2, 3, 4). Entry Wij
in the matrix W below is the weight of the edge {i, j}.
(0 1 8 1 4)
1 0 12 4 9
W =8 12 0 7 3
14 7 0 2
3 2 0
What is the minimum
spanning tree T in this graph such that vertex 0 is possible weight of a
a leaf node in the tree TT?
(A) 7
(B) 8
(C)9
(D) 10

2. In the graph given in question 1, what is the minimum possible weight of a


path P from vertex 1 to vertex 2 in this graph such that P contains at most 3
edges?
(A) 7
(B) 8
(C) 9
(D) 10
3. The degree sequence of a simple graph is the sequence of the degrees of
the nodes in the graph in decreasing order. Which of the following sequences
can not be the degree sequence of any graph?
I. 7, 6, 5, 4, 4, 3, 2, 1
II. 6, 6, 6, 6, 3, 3, 2, 2
III. 7, 6, 6, 4, 4, 3, 2, 2
IV. 8, 7, 7, 6, 4, 2, 1, 1
(A) I andIl
(B) Ill and IV
(C) IV only
(D) Il and IV
node is 5.
maximum number of keys in a
4. Consider a Bt-tree in which the
in any non-root node?
What is the minimum number of keys
10
(A) 1
(B) 2
(C) 3
(D) 4 It
as input argument.
1. The C function takes a simply-linked list
following returns
last element to the front
of the list and
modifies the list by moving thethe code is left blank.
the modified list. Some part of
typedef struct node

int value;
struct node *next;
Node;
Node move_to_front (Node *head)

Node *p, *q; NULL) )


if ((head = = NULL: || (head->next
==

return head;
q NULL; p = head;
while (p-> next !=NULL)

p p-next;

return head;

the blank line.


Choose the correct alternative to replace
(A) q NULL; p->next
=
=
head; head = p;
p; p->next head;
=

(B) q->next NULL; head


= =

NULL;
=

(C) head P: p->next q; q->next head


= =

(D) q->next NULL; p->next head;


=
=
=p;
of 10 uses open addressing with
hash function h(k)=k
2. A hash table length
inserting 6 values into an empty hash table,
mod 10, and linear probing. After
the table is as shown below.

42
23
34
52
46
33
8
9
Which one of the following choices gives a possible
order in which the key
values could have been inserted in the table?
(A) 46, 42, 34, 52, 23, 33
11
(B) 34, 42, 23, 52, 33, 46
(C) 46, 34, 42, 23, 52, 33
(D) 42, 46, 33, 23, 34, 52
3. How
many different insertion
hash function and linear sequences of the
(A) 10 probing will result in the key values using the
same
hash table shown above?
(B) 20
(C) 30
(D) 40
1. Which one of the
search trees for following is a key factor for
indexing preferring B-trees to binary
(a) Database relations havedatabase relations?
a
large number of records
(b) Database relations are sorted
(c) B-trees require less memory than the primary key
on

binary search trees


(d) Data transfer form disks is in blocks.
2. How many distinct
(a) 5
binary search trees can be created out of 4
distinct keys?
(b) 14
(c)24
(d) 42
3. In a
complete k-ary tree, every internal node has
number of leaves in such a exactly k children. The
(a) nk
tree withn internal nodes is:
(6) (n 1) k+ 1
(c) n(k-1) +1
(d) n(k - 1)
4) Suppose T(n) = 2T(n/2) + n, T(0) = T(1) = 1
Which one of the following is false.
a) T(n) = O(n^2)
b) T(n) = O(nLogn)
c) T(n) = Q(n)
d) T(n) =O(nLogn)
1) A program P reads in 500 integers in the range [0..100] representing the
scores of 500 students. It then prints the frequency of each score above 50.
What would be the best way for P to store the frequencies?
(a) An array of 50 numbers
(b) An array of 100 numbers
(c) An array of 500 numbers
(d) A dynamically allocated array of 550 numbers
2) An undirected graph G has n nodes. Its adjacency matrix is given by an n x
n square matrix whose (i) diagonal elements are 0's and (i) non-diagonal
elements are 1's. which one of the following is TRUE?
(a) Graph G has no minimum spanning tree (MST)
(b) Graph G has a unique MST of cost n-1
(c) Graph G has multiple distinct MSTs, each of cost n-1
(d) Graph G has multiple spanning trees of different costs
3) of
The time complexity computing the transitive closure of a binary relation
on a set of n elements is known to Be:
a) O(n)
b) O(nLogn)
12
c)OM(3/2))
d) O(n^3)
4. A
Priority-Queue is implemented as a Max-Heap. Initially, it has 5 elements.
The level-order traversal of the
heap is given below:
0,8,5, 3, 2
Two new elements "1' and "7' are inserted in the heap in that order. Thelevel
order traversal of the heap after the insertion of the elements is:
(a) 10, 8, 7, 5, 3, 2, 1
(6),10, 8, 7,2, 3, 1,5
(c) 10, 8, 7,1, 2, 3, 5
(d) 10, 8, 7,3, 2, 1, 5
1. The subset-sum problem is defined as follows. Given a set of n positive
integers,S = and positive integer W, is there a subset of S
{al ,a2 ,a3 ...,an}
whose elements sum to W? A dynamic program for solving this problem uses

n rows and W+1 columns. X[i, j],1<=i<=


a 2-dimensional Boolean array X, withthere is a subset of fal,a2 ,..,ai} whose
n, 0=j<= W, is TRUE if and only if
elements sum to j. Which of the following is
valid for 2 = i <=n and ai <=j<=
W?
(A) X[i. j] = X[i - 1, j] V X[, j -ai]

(B) X[. j] X[i 1, j] V X[i 1, j ai]


-

= -

(C) X[i. j] = X[i - 1, j]V X,j-ail

(D) X[i. j] =
X[i 1, j] VX[i -1, j-ail
-

that attempts to locate an element x in an


Consider the following C program
search. The program is
erroneous.

array Y using binary


1. f(int Y[10], int x) {
2. int i, j, k;
3. i 0; j =9;
do {
k = (i + j) /2;
5. else j =

6. if( Y[k] < x) i = k;


k while(Y[k] != x && i < j);
in the array
8. if(Y[k] x) printf ("x is
==

else printf (" x is not in the array


9.
") ;
10. 1
Run on IDE fail?
contents of Y and x does the program
On which of the following 13 15 17 19] and x 1
<

and x < 10 (B) Yis [1 3 57911


(A) Yis [123456 78910]and
(C) Yis [2222222222] x >2 even Answer (C)
The
10 12 14 16 18 20] and 2 <x< 20 and x is last
4 6 8 searched is the
(D) Y is [2 work for the cases
where element to be
above program doesn't in YI. For
than the last element (or maximum element)
element of Y] or greater value as k in all
in an infinite loop because i is assigned
such cases, program goes So while condition
never

becomes equal to or greater than j.


iterations, and i never
becomes false. to make it work properlyy
the correction needed in the program
4. In question 3,
is line 6 to: if (Y[K]<
i =k+ 1; elsej = k-1; (B) Change
(A) Change line 6 to: (Y[k] x)
if <
13
X)i =k-1; else j =
k+1; (C) Change line 6 to: if (Y[K] x) i k; else j k; (D)
<= = =

corrected
Change line 7 to: } while ((Y[k] ==
x) && i <
j); Answer (A) Below is the
function
f(int Y[10], int x) {
int i, j, k;
i = 0; j = 9;
do {
k =
(i +
j) /2;
i f Y[k] <
x) i =k + 1; else j =
k
- 1;

while(Y[k] !=x && i < j);

if(Y[k]= x) printf ("x is in the array


")
else printf (" x is not in the array ")

You might also like