DS SPT Lecture SK
DS SPT Lecture SK
Data Structure
Samir Kariya
Que-1
Following is C like pseudo code of a function that takes a number as an
argument, and uses a stack S to do processing.
void fun(int n)
{
Stack S; // Say it creates an empty stack S
while (n > 0)
{ // This line pushes the value of n%2 to stack S
push(&S, n%2);
n = n/2;
} // Run while Stack S is not empty
while (!isEmpty(&S))
printf("%d ", pop(&S)); // pop an element from S and print it
}
What does the above function do in general?
(A) Prints binary representation of n in reverse order
(B) Prints binary representation of n
(C) Prints the value of Log n
(D) Prints the value of Log n in reverse order
Que-1 Ans
Following is C like pseudo code of a function that takes a number as an
argument, and uses a stack S to do processing.
void fun(int n)
{
Stack S; // Say it creates an empty stack S
while (n > 0)
{ // This line pushes the value of n%2 to stack S
push(&S, n%2);
n = n/2;
} // Run while Stack S is not empty
while (!isEmpty(&S))
printf("%d ", pop(&S)); // pop an element from S and print it
}
What does the above function do in general?
(A) Prints binary representation of n in reverse order
(B) Prints binary representation of n
(C) Prints the value of Log n
(D) Prints the value of Log n in reverse order
Que-2
Which of the following is true about linked list
implementation of stack?
(A) In push operation, if new nodes are inserted at the
beginning of linked list, Then in pop operation, nodes
must be removed from end.
(B) In push operation, if new nodes are inserted at the end,
then in pop operation, nodes must be removed from the
beginning.
(C) Both of the above
(D) None of the above
Que-2 Ans
Which of the following is true about linked list
implementation of stack?
(A) In push operation, if new nodes are inserted at the
beginning of linked list, Then in pop operation, nodes
must be removed from end.
(B) In push operation, if new nodes are inserted at the end,
then in pop operation, nodes must be removed from the
beginning.
(C) Both of the above
(D) None of the above
Que-3
The following postfix expression with single digit operands
is evaluated using a stack:
931^/23*+51*-
Note that ^ is the exponentiation operator. The top two
elements of the stack after the first * is evaluated are:
(A) 3,6
(B) 9, 5
(C) 9, 6
(D) 3,5
Que-3 Ans
The following postfix expression with single digit operands
is evaluated using a stack:
931^/23*+51*-
Note that ^ is the exponentiation operator. The top two
elements of the stack after the first * is evaluated are:
(A) 3,6
(B) 9, 5
(C) 9, 6
(D) 3,5
Que-4
• Consider the usual algorithm for determining whether a
sequence of parentheses is balanced.
The maximum number of parentheses that appear on
the stack AT ANY ONE TIME when the algorithm analyzes:
( ( ) ( ( ) ) ( ( ) ) ) are:
(A) 1
(B) 2
(C) 3
(D) 4
Que-4 Ans
• Consider the usual algorithm for determining whether a
sequence of parentheses is balanced.
The maximum number of parentheses that appear on
the stack AT ANY ONE TIME when the algorithm analyzes:
( ( ) ( ( ) ) ( ( ) ) ) are:
(A) 1
(B) 2
(C) 3
(D) 4
Que-5
• Here is an infix expression: 6 + 5 * ( 8 * 5 – 14 ). Suppose
that we are using the usual stack algorithm to convert
the expression from infix to postfix notation. The
maximum number of symbols that will appear on the
stack AT ONE TIME during the conversion of this
expression?
(A) 1
(B) 2
(C) 3
(D) 4
Que-5 Ans
• Here is an infix expression: 6 + 5 * ( 8 * 5 – 14 ). Suppose
that we are using the usual stack algorithm to convert
the expression from infix to postfix notation. The
maximum number of symbols that will appear on the
stack AT ONE TIME during the conversion of this
expression?
(A) 1
(B) 2
(C) 3
(D) 4
Que-6
• What should be the growing direction of two stacks while
implementing them in a similar array so as to reduce the
overflow chances?
(A) Forward
(B) Backward
(C) Opposite
(D) None of the above
Que-6 Ans
• What should be the growing direction of two stacks while
implementing them in a similar array so as to reduce the
overflow chances?
(A) Forward
(B) Backward
(C) Opposite
(D) None of the above
Que-7
What is the postfix form of the following prefix *+ab-cd
(A) Abcd+*-
(B) Ab+*cd-
(C) Ab+cd-*
(D) Ab+-cd*
Que-7 Ans
What is the postfix form of the following prefix *+ab-cd
(A) Abcd+*-
(B) Ab+*cd-
(C) Ab+cd-*
(D) Ab+-cd*
Que-8
• Which of the following permutation can be obtained
in the output (in the same order) using a stack
assuming that the input is the sequence1, 2, 3, 4, 5 in
that order?
(A) 3, 4, 5, 1, 2
(B) 1, 5, 2, 3, 4
(C) 5, 4, 3, 1, 2
(D) 3, 4, 2, 5, 1
Que-8 Ans
• Which of the following permutation can be obtained
in the output (in the same order) using a stack
assuming that the input is the sequence1, 2, 3, 4, 5 in
that order?
(A) 3, 4, 5, 1, 2
(B) 1, 5, 2, 3, 4
(C) 5, 4, 3, 1, 2
(D) 3, 4, 2, 5, 1
Que-9
• The following sequence of operations is performed
on a stack : PUSH (1), PUSH (2), POP, PUSH (1), PUSH
(2), POP, POP, POP, PUSH (2), POP.
• The sequence of values popped out is:
(A) 2, 1, 2, 1, 2
(B) 2, 2, 1, 1, 2
(C) 1, 2,2,1,2
(D) 2, 2, 1, 2, 1
(E) None of the above
Que-9 Ans
• The following sequence of operations is performed
on a stack : PUSH (1), PUSH (2), POP, PUSH (1), PUSH
(2), POP, POP, POP, PUSH (2), POP.
• The sequence of values popped out is:
(A) 2, 1, 2, 1, 2
(B) 2, 2, 1, 1, 2
(C) 1, 2, 2, 1, 2
(D) 2, 2, 1, 2, 1
(E) None of the above
Que-10
• Suppose one character at a time comes as an input
from a string of letters. There is an option either to
(i) print the incoming letter or to
(ii) put the incoming letter on to a stack.
• Also a letter from top of the stack can be popped out
at any time and printed.
• The total number of total distinct words that can be
formed out of a string of three letters in this fashion,
is A B C.
(A) 3
(B) 4
(C) 5
(D) 6
Que-10 Ans
• Suppose one character at a time comes as an input
from a string of letters. There is an option either to
(i) print the incoming letter or to
(ii) put the incoming letter on to a stack.
• Also a letter from top of the stack can be popped out
at any time and printed.
• The total number of total distinct words that can be
formed out of a string of three letters in this fashion,
is A B C.
(A) 3
(B) 4
(C) 5 (ABC, ACB, BCA, BAC, CBA)
(D) 6
Que-11
The seven elements A, B, C, D, E, F and G are pushed
onto a stack in reverse order, i.e., starting from G.
The stack is popped five times and each element is
inserted into a queue.
Two elements are deleted from the queue and
pushed back onto the stack. Now, one element is
popped from the stack. The popped item is
________.
(A) F
(B) B
(C) C
(D) E
Que-11 Ans
The seven elements A, B, C, D, E, F and G are pushed
onto a stack in reverse order, i.e., starting from G.
The stack is popped five times and each element is
inserted into a queue.
Two elements are deleted from the queue and
pushed back onto the stack. Now, one element is
popped from the stack. The popped item is
________.
(A) F
(B) B
(C) C
(D) E
Que-12
• For Circular Queue of Length 4, front=1, rear=2 and
queue = _A_, _B_, ___, ____
Give front, rear and queue after the following operations.
Insert C, Delete, Insert D, Delete, Insert E, Insert F, Delete
(A) 1
(B) 3
(C) 4
(D) 5
Que-15 Ans
• The maximum number of binary trees that can be
formed with three unlabeled nodes is:
(A) 1
(B) 3
(C) 4
(D) 5
Que-16
• Consider a node X in a Binary Tree. Given that X has two
children, let Y be Inorder successor of X. Which of the
following is true about Y?
(A) 2^h -1
(B) 2^h-1 -1
(C) 2^h+1 -1
(D) 2^h+1
Que-17 Ans
• 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) 2^h -1
(B) 2^h-1 -1
(C) 2^h+1 -1
(D) 2^h+1
Que-18
• The height of a tree is the length of the longest root-to-
leaf path in it. The maximum and minimum number of
nodes in a binary tree of height 5 are
(A) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(B) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(C) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(D) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
Que-19 Ans
• Postorder traversal of a given binary search tree, T
produces the following sequence of keys 10, 9, 23, 22,
27, 25, 15, 50, 95, 60, 40, 29 Which one of the following
sequences of keys can be the result of an in-order
traversal of the tree T?
(A) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(B) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(C) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(D) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
Que-20
• The following three are known to be the preorder, inorder
and postorder sequences of a binary tree. But it is not
known which is which.
MBCAFHPYK
KAMCBYPFH
MABCKYFPH
Pick the true statement from the following.
(A) 53124786
(B) 53126487
(C) 53241678
(D) 53124768
Que-22 Ans
• A binary search tree contains the values 1, 2, 3, 4, 5, 6, 7,
8. The tree is traversed in pre-order and the values are
printed out. Which of the following sequences is a valid
output?
(A) 53124786
(B) 53126487
(C) 53241678
(D) 53124768
Que-23
• The in-order and pre-order traversal of a binary tree are
d b e a f c g and a b d e c f g respectively. The post order
traversal of a binary tree is
(A) e d b g f c a
(B) e d b f g c a
(C) d e b f g c a
(D) d e f g b c a
Que-23 Ans
• The in-order and pre-order traversal of a binary tree are
d b e a f c g and a b d e c f g respectively. The post order
traversal of a binary tree is
(A) e d b g f c a
(B) e d b f g c a
(C) d e b f g c a
(D) d e f g b c a
Que-24
• Find the inorder and postorder of the binary tree with the given
preorder: 60, 40, 20, 10, 30, 33, 50, 44, 51, 90, 70, 65, 80, 110,
100, 95, 99, 120.
(A) In order: 110, 100, 99, 90, 80, 70, 65, 60, 51, 50, 44, 40, 33, 30,
20, 10. Postorder: 110, 120, 100, 95, 99, 70, 80, 65, 60, 40, 50,
51, 44, 20, 30, 33, 10
(B) Inorder: 10, 20, 30, 33, 40, 44, 50, 51, 60, 65, 70, 80, 90, 95, 99,
100, 110, 120 Postorder: 10, 33, 30, 20, 44, 51, 50, 40, 65, 80,
70, 99, 95, 100, 120, 110, 90, 60
(C) In order: 10, 33, 30, 20, 44, 51, 50, 40, 60, 65, 80, 70, 99, 95,
100, 120, 110, Postorder: 10, 20, 30, 33, 40, 44, 50, 51, 60, 65,
70, 80, 90, 95, 99, 100, 110
(D) In order: 10, 33, 30, 20, 44, 51, 60, 65, 80, 70, 99, 95, 100, 120,
110, Postorder: 110, 100, 99, 90, 80, 70, 65, 60, 51, 50, 44, 40,
33, 30, 20, 10
Que-24 Ans
• Find the inorder and postorder of the binary tree with the given
preorder: 60, 40, 20, 10, 30, 33, 50, 44, 51, 90, 70, 65, 80, 110,
100, 95, 99, 120.
(A) In order: 110, 100, 99, 90, 80, 70, 65, 60, 51, 50, 44, 40, 33, 30,
20, 10. Postorder: 110, 120, 100, 95, 99, 70, 80, 65, 60, 40, 50,
51, 44, 20, 30, 33, 10
(B) Inorder: 10, 20, 30, 33, 40, 44, 50, 51, 60, 65, 70, 80, 90, 95, 99,
100, 110, 120 Postorder: 10, 33, 30, 20, 44, 51, 50, 40, 65, 80,
70, 99, 95, 100, 120, 110, 90, 60
(C) In order: 10, 33, 30, 20, 44, 51, 50, 40, 60, 65, 80, 70, 99, 95,
100, 120, 110, Postorder: 10, 20, 30, 33, 40, 44, 50, 51, 60, 65,
70, 80, 90, 95, 99, 100, 110
(D) In order: 10, 33, 30, 20, 44, 51, 60, 65, 80, 70, 99, 95, 100, 120,
110, Postorder: 110, 100, 99, 90, 80, 70, 65, 60, 51, 50, 44, 40,
33, 30, 20, 10
Que-25
• A binary search tree is generated by inserting in order the
following integers:
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
The number of nodes in the left subtree and right subtree
of the root is respectively is
(A) (4, 7)
(B) (7, 4)
(C) (8, 3)
(D) (3, 8)
Que-25 Ans
• A binary search tree is generated by inserting in order the
following integers:
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
The number of nodes in the left subtree and right subtree
of the root is respectively is
(A) (4, 7)
(B) (7, 4)
(C) (8, 3)
(D) (3, 8)
Que-26
• How is an insertion of a node into an AVL tree carried
out?
(C) The cost of searching an AVL tree is θ(n log n) but that of
a binary search tree is O(n)
(C) The cost of searching an AVL tree is θ(n log n) but that of
a binary search tree is O(n)
(A) 1
(B) 3
(C) 7
(D) 8
Que-28 Ans
In the balanced binary tree in Fig. given below, how
many nodes will become unbalanced when a node is
inserted as a child of the node “g”?
(A) 1
(B) 3
(C) 7
(D) 8
Que-29
• The following insertions are made to an initially empty B-
tree of order 5.
1,12 ,8, 2, 25, 5, 14, 28, 17
now the root node contains the element/elements
(A) 8
(B) 1
(C) 8, 17
(D) 12
Que-29 Ans
• The following insertions are made to an initially empty B-
tree of order 5.
1,12 ,8, 2, 25, 5, 14, 28, 17
now the root node contains the element/elements
(A) 8
(B) 1
(C) 8, 17
(D) 12
Que-30
• The number of interchanges required to sort 5,1,6,2,4 in
ascending order using Bubble Sort is
(A) 5
(B) 6
(C) 7
(D) 8
Que-30 Ans
• The number of interchanges required to sort 5,1,6,2,4 in
ascending order using Bubble Sort is
(A) 5
(B) 6
(C) 7
(D) 8
Que-31
• The minimum number of interchanges needed to convert
the array
89, 19, 40, 17, 12, 10, 2, 5, 7, 11, 6, 9, 70 into a heap with
the maximum element at the root node is
(A) 0
(B) 1
(C) 2
(D) 3
Que-31 Ans
• The minimum number of interchanges needed to convert
the array
89, 19, 40, 17, 12, 10, 2, 5, 7, 11, 6, 9, 70 into a heap with
the maximum element at the root node is
(A) 0
(B) 1
(C) 2
(D) 3
Que-32
• Consider a binary max-heap implemented using an array.
Which one of the following array represents a binary max-
heap?
(A) MNOPQR
(B) NQMPOR
(C) QMNPRO
(D) QMNPOR
Que-34 Ans
• The Breadth First Search algorithm has been
implemented using the queue data structure. One
possible order of visiting the nodes of the following graph
is
(A) MNOPQR
(B) NQMPOR
(C) QMNPRO
(D) QMNPOR
Que-35
Consider following graph
• Among the
following sequences:
(i) a b e g h f
(ii) a b f e h g
(iii) a b f h g e
(iv) a f g h b e
• Which are depth first traversals of the above graph?
(A) I, II and IV only (B) I and IV only
(C) II, III and IV only (D) I, III and IV only
Que-36
• Suppose we are sorting an array of eight integers using
quicksort, and we have just finished the first partitioning
with the array looking like this:
2 5 1 7 9 12 11 10
Which statement is correct?
(A) N^2
(B) N log N
(C) N
(D) N (log N)^2
Que-38
• 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 comparisons made by P for the
inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, 2} respectively. Which
one of the following holds?
(A) T1 = 5
(B) T1 < T2
(C) T1 > T2
(D) T1 = T2
Que-39
• The worst case running times of Insertion sort, Merge sort
and Quick sort, respectively, are:
(A) 4 5 3 2 1 3 4 5 2 1 2 3 4 5 1 1 2 3 4 5
(B) 5 4 3 1 2 5 4 1 2 3 5 1 2 3 4 1 2 3 4 5
(C) 4 3 2 1 5 3 2 1 5 4 2 1 5 4 3 1 5 4 3 2
(D) 4 5 3 2 1 2 3 4 5 1 3 4 5 2 1 1 2 3 4 5