Data Structures and Algorithms
Data Structures and Algorithms
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;
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;
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++)
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
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
of n MultiDequeue)
time complexity of a sequence
What is the worst case
discs is
problem with n
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 ==
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);
=
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)
(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
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
int value;
struct node *next;
Node;
Node move_to_front (Node *head)
return head;
q NULL; p = head;
while (p-> next !=NULL)
p p-next;
return head;
NULL;
=
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
= -
(D) X[i. j] =
X[i 1, j] VX[i -1, j-ail
-
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;