Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structure Questions and Answers

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Data Structure Questions and Answers –

Stack Operations – 1
This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Stack
Operations – 1”.

1. Process of inserting an element in stack is called ____________


a) Create
b) Push
c) Evaluation
d) Pop
View Answer

Answer: b
Explanation: Push operation allows users to insert elements in stack. If stack is filled completely
and trying to perform push operation stack – overflow can happen.

2. Process of removing an element from stack is called __________


a) Create
b) Push
c) Evaluation
d) Pop
View Answer

Answer: d
Explanation: Elements in stack are removed using pop operation. Pop operation removes the top
most element in the stack i.e. last entered element.

3. In a stack, if a user tries to remove an element from empty stack it is called _________
a) Underflow
b) Empty collection
c) Overflow
d) Garbage Collection
View Answer

Answer: a
Explanation: Underflow occurs when the user performs a pop operation on an empty stack.
Overflow occurs when the stack is full and the user performs a push operation. Garbage
Collection is used to recover the memory occupied by objects that are no longer used.

4. Pushing an element into stack already having five elements and stack size of 5, then stack
becomes
a) Overflow
b) Crash
c) Underflow
d) User flow
View Answer

Answer: a
Explanation: The stack is filled with 5 elements and pushing one more element causes a stack
overflow. This results in overwriting memory, code and loss of unsaved work on the computer.

5. Entries in a stack are “ordered”. What is the meaning of this statement?


a) A collection of stacks is sortable
b) Stack entries may be compared with the ‘<‘ operation
c) The entries are stored in a linked list
d) There is a Sequential entry that is one by one
View Answer

Answer : d
Explanation: In stack data structure, elements are added one by one using push operation. Stack
follows LIFO Principle i.e. Last In First Out(LIFO).

6. Which of the following applications may use a stack?


a) A parentheses balancing program
b) Tracking of local variables at run time
c) Compiler Syntax Analyzer
d) Data Transfer between two asynchronous process
View Answer

Answer: d
Explanation: Data transfer between the two asynchronous process uses the queue data structure
for synchronisation. The rest are all stack applications.

7. 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 or more
View Answer

Answer: c
Explanation: In the entire parenthesis balancing method when the incoming token is a left
parenthesis it is pushed into stack. A right parenthesis makes pop operation to delete the
elements in stack till we get left parenthesis as top most element. 3 elements are there in stack
before right parentheses comes. Therefore, maximum number of elements in stack at run time is
3.
8. Consider the usual algorithm for determining whether a sequence of parentheses is balanced.
Suppose that you run the algorithm on a sequence that contains 2 left parentheses and 3 right
parentheses (in some order).
The maximum number of parentheses that appear on the stack AT ANY ONE TIME during the
computation?
a) 1
b) 2
c) 3
d) 4 or more
View Answer

Answer: b
Explanation: In the entire parenthesis balancing method when the incoming token is a left
parenthesis it is pushed into stack. A right parenthesis makes pop operation to delete the
elements in stack till we get left parenthesis as top most element. 2 left parenthesis are pushed
whereas one right parenthesis removes one of left parenthesis. 2 elements are there before right
parenthesis which is the maximum number of elements in stack at run time.

9. What is the value of the postfix expression 6 3 2 4 + – *:


a) 1
b) 40
c) 74
d) -18
View Answer

Answer: d
Explanation: Postfix Expression is (6+(3-(2*4))) which results -18 as output.

10. Here is an infix expression: 4 + 3*(6*3-12). 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
View Answer

Answer: d
Explanation: When we perform the conversion from infix to postfix expression +, *, (, * symbols
are placed inside the stack. A maximum of 4 symbols are identified during the entire conversion.

This set of Data Structure Interview Questions and Answers focuses on “Stack Operations – 2”.
1. The postfix form of the expression (A+ B)*(C*D- E)*F / G is?
a) AB+ CD*E – FG /**
b) AB + CD* E – F **G /
c) AB + CD* E – *F *G /
d) AB + CDE * – * F *G /
View Answer

Answer: c
Explanation: (((A+ B)*(C*D- E)*F) / G) is converted to postfix expression as
(AB+(*(C*D- E)*F )/ G)
(AB+CD*E-*F) / G
(AB+CD*E-*F * G/). Thus Postfix expression is AB+CD*E-*F*G/

2. The data structure required to check whether an expression contains balanced parenthesis is?
a) Stack
b) Queue
c) Array
d) Tree
View Answer

Answer: a
Explanation: The stack is a simple data structure in which elements are added and removed
based on the LIFO principle. Open parenthesis is pushed into the stack and a closed parenthesis
pops out elements till the top element of the stack is its corresponding open parenthesis. If the
stack is empty, parenthesis is balanced otherwise it is unbalanced.

3. What data structure would you mostly likely see in a non recursive implementation of a
recursive algorithm?
a) Linked List
b) Stack
c) Queue
d) Tree
View Answer

Answer: b
Explanation: In recursive algorithms, the order in which the recursive process comes back is the
reverse of the order in which it goes forward during execution. The compiler uses the stack data
structure to implement recursion. In the forwarding phase, the values of local variables,
parameters and the return address are pushed into the stack at each recursion level. In the
backing-out phase, the stacked address is popped and used to execute the rest of the code.

4. The process of accessing data stored in a serial access memory is similar to manipulating data
on a ________
a) Heap
b) Binary Tree
c) Array
d) Stack
View Answer

Answer: d
Explanation: In serial access memory data records are stored one after the other in which they are
created and are accessed sequentially. In stack data structure, elements are accessed sequentially.
Stack data structure resembles the serial access memory.

5. The postfix form of A*B+C/D is?


a) *AB/CD+
b) AB*CD/+
c) A*BC+/D
d) ABCD+/*
View Answer

Answer: b
Explanation: Infix expression is (A*B)+(C/D)
AB*+(C/D)
AB*CD/+. Thus postfix expression is AB*CD/+.

6. Which data structure is needed to convert infix notation to postfix notation?


a) Branch
b) Tree
c) Queue
d) Stack
View Answer

Answer: d
Explanation: The Stack data structure is used to convert infix expression to postfix expression.
The purpose of stack is to reverse the order of the operators in the expression. It also serves as a
storage structure, as no operator can be printed until both of its operands have appeared.

7. The prefix form of A-B/ (C * D ^ E) is?


a) -/*^ACBDE
b) -ABCD*^DE
c) -A/B*C^DE
d) -A/BC*^DE
View Answer

Answer: c
Explanation: Infix Expression is (A-B)/(C*D^E)
(-A/B)(C*D^E)
-A/B*C^DE. Thus prefix expression is -A/B*C^DE

8. What is the result of the following operation?


Top (Push (S, X))
a) X
b) X+S
c) S
d) XS
View Answer

Answer: a
Explanation: The function Push(S,X) pushes the value X in the stack S. Top() function gives the
value which entered last. X entered into stack S at last.

9. The prefix form of an infix expression (p + q) – (r * t) is?


a) + pq – *rt
b) – +pqr * t
c) – +pq * rt
d) – + * pqrt
View Answer

Answer: c
Explanation: Given Infix Expression is ((p+q)-(r*t))
(+pq)-(r*t)
(-+pq)(r*t)
-+pq*rt. Thus prefix expression is -+pq*rt.

10. Which data structure is used for implementing recursion?


a) Queue
b) Stack
c) Array
d) List
View Answer

Answer: b
Explanation: Stacks are used for the implementation of Recursion.

1. The result of evaluating the postfix expression 5, 4, 6, +, *, 4, 9, 3, /, +, * is?


a) 600
b) 350
c) 650
d) 588
View Answer

Answer: b
Explanation: The postfix expression is evaluated using stack. We will get the infix expression as
(5*(4+6))*(4+9/3). On solving the Infix Expression, we get
(5*(10))*(4+3)
= 50*7
= 350.
2. Convert the following infix expressions into its equivalent postfix expressions
(A + B ⋀D)/(E – F)+G
a) (A B D ⋀ + E F – / G +)
b) (A B D +⋀ E F – / G +)
c) (A B D ⋀ + E F/- G +)
d) (A B D E F + ⋀ / – G +)
View Answer

Answer: a
Explanation: The given infix expression is (A + B ⋀D)/(E – F)+G.
(A B D ^ + ) / (E – F) +G
(A B D ^ + E F – ) + G. ‘/’ is present in stack.
A B D ^ + E F – / G +. Thus Postfix Expression is A B D ^ + E F – / G +.

3. Convert the following Infix expression to Postfix form using a stack


x + y * z + (p * q + r) * s, Follow usual precedence rule and assume that the expression is legal.
a) xyz*+pq*r+s*+
b) xyz*+pq*r+s+*
c) xyz+*pq*r+s*+
d) xyzp+**qr+s*+
View Answer

Answer: a
Explanation: The Infix Expression is x + y * z + (p * q + r) * s.
(x y z ) + (p * q + r) * s. ‘+’, ‘*’ are present in stack.
(x y z * + p q * r) * s. ‘+’ is present in stack.
x y z * + p q * r + s * +. Thus Postfix Expression is x y z * + p q * r + s * +.

4. Which of the following statement(s) about stack data structure is/are NOT correct?
a) Linked List are used for implementing Stacks
b) Top of the Stack always contain the new node
c) Stack is the FIFO data structure
d) Null link is present in the last node at the bottom of the stack
View Answer

Answer: c
Explanation: Stack follows LIFO.

5. Consider the following operation performed on a stack of size 5.


Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
Pop();
Push(5);
After the completion of all operation, the number of elements present in stack are
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: a
Explanation: Number of elements present in stack is equal to the difference between number of
push operations and number of pop operations. Number of elements is 5-4=1.

6. Which of the following is not an inherent application of stack?


a) Reversing a string
b) Evaluation of postfix expression
c) Implementation of recursion
d) Job scheduling
View Answer

Answer: d
Explanation: Job Scheduling is not performed using stacks.

7. The type of expression in which operator succeeds its operands is?


a) Infix Expression
b) Prefix Expression
c) Postfix Expression
d) Both Prefix and Postfix Expressions
View Answer

Answer: c
Explanation: The expression in which operator succeeds its operands is called postfix expression.
The expression in which operator precedes the operands is called prefix expression. If an
operator is present between two operands, then it is called infix expressions.

8. Assume that the operators +,-, X are left associative and ^ is right associative.
The order of precedence (from highest to lowest) is ^, X, +, -. The postfix expression for the
infix expression a + b X c – d ^ e ^ f is
a) abc X+ def ^^ –
b) abc X+ de^f^ –
c) ab+c Xd – e ^f^
d) -+aXbc^ ^def
View Answer

Answer: b
Explanation: Given Infix Expression is a + b X c – d ^ e ^ f.
(a b c X +) (d ^ e ^ f). ‘–‘ is present in stack.
(a b c X + d e ^ f ^ -). Thus the final expression is (a b c X + d e ^ f ^ -).

9. If the elements “A”, “B”, “C” and “D” are placed in a stack and are deleted one at a time, what
is the order of removal?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
View Answer

Answer: b
Explanation: Stack follows LIFO(Last In First Out). So the removal order of elements are
DCBA.

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Queue
Operations”.

1. A linear list of elements in which deletion can be done from one end (front) and insertion can
take place only at the other end (rear) is known as a ?
a) Queue
b) Stack
c) Tree
d) Linked list
View Answer

Answer: a
Explanation: Linear list of elements in which deletion is done at front side and insertion at rear
side is called Queue. In stack we will delete the last entered element first.

2. The data structure required for Breadth First Traversal on a graph is?
a) Stack
b) Array
c) Queue
d) Tree
View Answer

Answer: c
Explanation: In Breadth First Search Traversal, BFS, starting vertex is first taken and adjacent
vertices which are unvisited are also taken. Again, the first vertex which was added as an
unvisited adjacent vertex list will be considered to add further unvisited vertices of the graph. To
get first unvisited vertex we need to follows First In First Out principle. Queue uses FIFO
principle.
3. A queue follows __________
a) FIFO (First In First Out) principle
b) LIFO (Last In First Out) principle
c) Ordered array
d) Linear tree
View Answer

Answer: a
Explanation: Element first added in queue will be deleted first which is FIFO principle.

4. Circular Queue is also known as ________


a) Ring Buffer
b) Square Buffer
c) Rectangle Buffer
d) Curve Buffer
View Answer

Answer: a
Explanation: Circular Queue is also called as Ring Buffer. Circular Queue is a linear data
structure in which last position is connected back to the first position to make a circle. It forms a
ring structure.

5. If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a time, in
what order will they be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
View Answer

Answer: a
Explanation: Queue follows FIFO approach. i.e. First in First Out Approach. So, the order of
removal elements are ABCD.

6. A data structure in which elements can be inserted or deleted at/from both the ends but not in
the middle is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue
View Answer

Answer: c
Explanation: In dequeuer, we can insert or delete elements from both the ends. In queue, we will
follow first in first out principle for insertion and deletion of elements. Element with least
priority will be deleted in a priority queue.
7. A normal queue, if implemented using an array of size MAX_SIZE, gets full when
a) Rear = MAX_SIZE – 1
b) Front = (rear + 1)mod MAX_SIZE
c) Front = rear + 1
d) Rear = front
View Answer

Answer: a
Explanation: When Rear = MAX_SIZE – 1, there will be no space left for the elements to be
added in queue. Thus queue becomes full.

8. Queues serve major role in ______________


a) Simulation of recursion
b) Simulation of arbitrary linked list
c) Simulation of limited resource allocation
d) Simulation of heap sort
View Answer

Answer: c
Explanation: Simulation of recursion uses stack data structure. Simulation of arbitrary linked
lists uses linked lists. Simulation of resource allocation uses queue as first entered data needs to
be given first priority during resource allocation. Simulation of heap sort uses heap data
structure.

9. Which of the following is not the type of queue?


a) Ordinary queue
b) Single ended queue
c) Circular queue
d) Priority queue
View Answer

Answer: b
Explanation : Queue always has two ends. So, single ended queue is not the type of queue.

This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs)
focuses on “Rope”.

1. Which of the following is also known as Rope data structure?


a) Cord
b) String
c) Array
d) Linked List
View Answer
Answer: a
Explanation: Array is a linear data structure. Strings are a collection and sequence of codes,
alphabets or characters. Linked List is a linear data structure having a node containing data input
and the address of the next node. The cord is also known as the rope data structure.

2. Which type of data structure does rope represent?


a) Array
b) Linked List
c) Queue
d) Binary Tree
View Answer

Answer: d
Explanation: Rope is a special binary tree in which the end nodes contain the string and its
length. The array is a linear data structure. Linked List is a linear data structure having a node
containing data input and the address of the next node. The queue is a data structure working on
the principle of FIFO.

3. What is the time complexity for finding the node at x position where n is the length of the
rope?
a) O (log n)
b) O (n!)
c) O (n2)
d) O (1)
View Answer

Answer: a
Explanation: In order to find the node at x position in a rope data structure where N is the length
of the rope, we start a recursive search from the root node. So the time complexity for worst case
is found to be O (log N).

4. What is the time complexity for creating a new node and then performing concatenation in the
rope data structure?
a) O (log n)
b) O (n!)
c) O (n2)
d) O (1)
View Answer

Answer: d
Explanation: In order to perform the concatenation on the rope data structure, one can create two
nodes S1 and S2 and then performing the operation in constant time that is the time complexity
is O (1).

5. What is the time complexity for splitting the string into two new string in the rope data
structure?
a) O (n2)
b) O (n!)
c) O (log n)
d) O (1)
View Answer

Answer: c
Explanation: In order to perform the splitting on the rope data structure, one can split the given
string into two new string S1 and S2 in O (log n) time. So, the time complexity for worst case is
O (log n).

6. Which type of binary tree does rope require to perform basic operations?
a) Unbalanced
b) Balanced
c) Complete
d) Full
View Answer

Answer: b
Explanation: To perform the basic operations on a rope data structure like insertion, deletion,
concatenation and splitting, the rope should be a balanced tree. After performing the operations
one should again re-balance the tree.

7. What is the time complexity for inserting the string and forming a new string in the rope data
structure?
a) O (log n)
b) O (n!)
c) O (n2)
d) O (1)
View Answer

Answer: a
Explanation: In order to perform the insertion on the rope data structure, one can insert the given
string at any position x to form a new string in O (log n) time. So, the time complexity for worst
case is O (log n). This can be done by one split operation and two concatenation operations.

8. Is insertion and deletion operation faster in rope than an array?


a) True
b) False
View Answer

Answer: a
Explanation: In order to perform the insertion on the rope data structure, the time complexity is
O (log n). In order to perform the deletion on the rope data structure, the time complexity for
worst case is O (log n). While for arrays the time complexity is O (n).
9. What is the time complexity for deleting the string to form a new string in the rope data
structure?
a) O (n2)
b) O (n!)
c) O (log n)
d) O (1)
View Answer

Answer: c
Explanation: In order to perform the deletion on the rope data structure, one can delete the given
string at any position x to form a new string in O (log n) time. So, the time complexity for worst
case is O (log n). This can be done by two split operations and one concatenation operation.

10. Is it possible to perform a split operation on a string in the rope if the split point is in the
middle of the string.
a) True
b) False
View Answer

Answer: a
Explanation: In order to perform the splitting on the rope data structure, one can split the given
string into two new string S1 and S2 in O (log n) time. So, the time complexity for worst case is
O (log n). The split operation can be performed if the split point is either at the end of the string
or in the middle of the string.

1. What is the maximum number of children that a binary tree node can have?
a) 0
b) 1
c) 2
d) 3
View Answer

Answer: c
Explanation: In a binary tree, a node can have atmost 2 nodes (i.e.) 0,1 or 2 left and right child.

2. The following given tree is an example for?

a) Binary tree
b) Binary search tree
c) Fibonacci tree
d) AVL tree
View Answer

Answer: b
Explanation: The given tree is an example for binary search since the tree has got two children
and the left and right children do not satisfy binary search tree’s property.

3. A binary tree is a rooted tree but not an ordered tree.


a) true
b) false
View Answer

Answer: b
Explanation: A binary tree is a rooted tree and also an ordered tree (i.e) every node in a binary
tree has at most two children.

4. How many common operations are performed in a binary tree?


a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: Three common operations are performed in a binary tree- they are insertion,
deletion and traversal.

5. What is the traversal strategy used in the binary tree?


a) depth-first traversal
b) breadth-first traversal
c) random traversal
d) preorder traversal
View Answer

Answer: b
Explanation: Breadth first traversal, also known as level order traversal is the traversal strategy
used in a binary tree. It involves visiting all the nodes at a given level.

6. How many types of insertion are performed in a binary tree?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: Two kinds of insertion operation is performed in a binary tree- inserting a leaf node
and inserting an internal node.

7. What operation does the following diagram depict?

a) inserting a leaf node


b) inserting an internal node
c) deleting a node with 0 or 1 child
d) deleting a node with 2 children
View Answer

Answer: c
Explanation: The above diagram is a depiction of deleting a node with 0 or 1 child since the node
D which has 1 child is deleted.

8. General ordered tree can be encoded into binary trees.


a) true
b) false
View Answer

Answer: a
Explanation: General ordered tree can be mapped into binary tree by representing them in a left-
child-right-sibling way.

9. How many bits would a succinct binary tree occupy?


a) n+O(n)
b) 2n+O(n)
c) n/2
d) n
View Answer

Answer: b
Explanation: A succinct binary tree occupies close to minimum possible space established by
lower bounds. A succinct binary tree would occupy 2n+O(n) bits.
10. The average depth of a binary tree is given as?
a) O(N)
b) O(√N)
c) O(N2)
d) O(log N)
View Answer

Answer: d
Explanation: The average depth of a binary tree is given as O(√N). In case of a binary search
tree, it is O(log N).

1. The number of edges from the root to the node is called __________ of the tree.
a) Height
b) Depth
c) Length
d) Width
View Answer

Answer: b
Explanation: The number of edges from the root to the node is called depth of the tree.

2. The number of edges from the node to the deepest leaf is called _________ of the tree.
a) Height
b) Depth
c) Length
d) Width
View Answer

Answer: a
Explanation: The number of edges from the node to the deepest leaf is called height of the tree.

3. What is a full binary tree?


a) Each node has exactly zero or two children
b) Each node has exactly two children
c) All the leaves are at the same level
d) Each node has exactly one or two children
View Answer

Answer: a
Explanation: A full binary tree is a tree in which each node has exactly 0 or 2 children.

4. What is a complete binary tree?


a) Each node has exactly zero or two children
b) A binary tree, which is completely filled, with the possible exception of the bottom level,
which is filled from right to left
c) A binary tree, which is completely filled, with the possible exception of the bottom level,
which is filled from left to right
d) A tree In which all nodes have degree 2
View Answer

Answer: c
Explanation: A binary tree, which is completely filled, with the possible exception of the bottom
level, which is filled from left to right is called complete binary tree. A Tree in which each node
has exactly zero or two children is called full binary tree. A Tree in which the degree of each
node is 2 except leaf nodes is called perfect binary tree.

5. What is the average case time complexity for finding the height of the binary tree?
a) h = O(loglogn)
b) h = O(nlogn)
c) h = O(n)
d) h = O(log n)
View Answer

Answer: d
Explanation: The nodes are either a part of left sub tree or the right sub tree, so we don’t have to
traverse all the nodes, this means the complexity is lesser than n, in the average case, assuming
the nodes are spread evenly, the time complexity becomes O(logn).

6. Which of the following is not an advantage of trees?


a) Hierarchical structure
b) Faster search
c) Router algorithms
d) Undo/Redo operations in a notepad
View Answer

Answer: d
Explanation: Undo/Redo operations in a notepad is an application of stack. Hierarchical
structure, Faster search, Router algorithms are advantages of trees.

7. In a full binary tree if number of internal nodes is I, then number of leaves L are?
a) L = 2*I
b) L = I + 1
c) L = I – 1
d) L = 2*I – 1
View Answer

8. In a full binary tree if number of internal nodes is I, then number of nodes N are?
a) N = 2*I
b) N = I + 1
c) N = I – 1
d) N = 2*I + 1
View Answer
Answer: d
Explanation: Relation between number of internal nodes(I) and nodes(N) is N = 2*I+1.

9. In a full binary tree if there are L leaves, then total number of nodes N are?
a) N = 2*L
b) N = L + 1
c) N = L – 1
d) N = 2*L – 1
View Answer

Answer: d
Explanation: The relation between number of nodes(N) and leaves(L) is N=2*L-1.

10. Which of the following is incorrect with respect to binary trees?


a) Let T be a binary tree. For every k ≥ 0, there are no more than 2k nodes in level k
b) Let T be a binary tree with λ levels. Then T has no more than 2λ – 1 nodes
c) Let T be a binary tree with N nodes. Then the number of levels is at least ceil(log (N + 1))
d) Let T be a binary tree with N nodes. Then the number of levels is at least floor(log (N + 1))
View Answer

Answer: d
Explanation: In a binary tree, there are atmost 2k nodes in level k and 2k-1 total number of nodes.
Number of levels is at least ceil(log(N+1)).

1. Which of the following is false about a binary search tree?


a) The left child is always lesser than its parent
b) The right child is always greater than its parent
c) The left and right sub-trees should also be binary search trees
d) In order sequence gives decreasing order of elements
View Answer

Answer: d
Explanation: In order sequence of binary search trees will always give ascending order of
elements. Remaining all are true regarding binary search trees.

2. How to search for a key in a binary search tree?


a)

public Tree search(Tree root, int key)


{
if( root == null || root.key == key )
{
return root;
}
if( root.key < key )
{
return search(root.right,key);
}
else
return search(root.left,key);
}

b)

public Tree search(Tree root, int key)


{
if( root == null || root.key == key )
{
return root;
}
if( root.key < key )
{
return search(root.left,key);
}
else
return search(root.right,key);
}

c)

public Tree search(Tree root, int key)


{
if( root == null)
{
return root;
}
if( root.key < key )
{
return search(root.right,key);
}
else
return search(root.left,key);
}

d)

public Tree search(Tree root, int key)


{
if( root == null)
{
return root;
}
if( root.key < key )
{
return search(root.right.right,key);
}
else
return search(root.left.left,key);
}
View Answer
Answer: a
Explanation: As we know that the left child is lesser than the parent, if the root’s key is greater
than the given key, we look only into the left sub-tree, similarly for right sub-tree.

3. What is the speciality about the inorder traversal of a binary search tree?
a) It traverses in a non increasing order
b) It traverses in an increasing order
c) It traverses in a random fashion
d) It traverses based on priority of the node
View Answer

Answer: b
Explanation: As a binary search tree consists of elements lesser than the node to the left and the
ones greater than the node to the right, an inorder traversal will give the elements in an
increasing order.

4. What does the following piece of code do?

public void func(Tree root)


{
func(root.left());
func(root.right());
System.out.println(root.data());
}

a) Preorder traversal
b) Inorder traversal
c) Postorder traversal
d) Level order traversal
View Answer

Answer: c
Explanation: In a postorder traversal, first the left child is visited, then the right child and finally
the parent.

5. What does the following piece of code do?

public void func(Tree root)


{
System.out.println(root.data());
func(root.left());
func(root.right());
}

a) Preorder traversal
b) Inorder traversal
c) Postorder traversal
d) Level order traversal
View Answer

Answer: a
Explanation: In a preorder traversal, first the parent is visited, then the left child and finally the
right child.

6. How will you find the minimum element in a binary search tree?
a)

public void min(Tree root)


{
while(root.left() != null)
{
root = root.left();
}
System.out.println(root.data());
}

b)

public void min(Tree root)


{
while(root != null)
{
root = root.left();
}
System.out.println(root.data());
}

c)

public void min(Tree root)


{
while(root.right() != null)
{
root = root.right();
}
System.out.println(root.data());
}

d)

public void min(Tree root)


{
while(root != null)
{
root = root.right();
}
System.out.println(root.data());
}
View Answer
Answer: a
Explanation: Since all the elements lesser than a given node will be towards the left, iterating to
the leftmost leaf of the root will give the minimum element in a binary search tree.

7. How will you find the maximum element in a binary search tree?
a)

public void max(Tree root)


{
while(root.left() != null)
{
root = root.left();
}
System.out.println(root.data());
}

b)

public void max(Tree root)


{
while(root != null)
{
root = root.left();
}
System.out.println(root.data());
}

c)

public void max(Tree root)


{
while(root.right() != null)
{
root = root.right();
}
System.out.println(root.data());
}

d)

public void max(Tree root)


{
while(root != null)
{
root = root.right();
}
System.out.println(root.data());
}
View Answer
Answer: c
Explanation: Since all the elements greater than a given node are towards the right, iterating
through the tree to the rightmost leaf of the root will give the maximum element in a binary
search tree.

8. What are the worst case and average case complexities of a binary search tree?
a) O(n), O(n)
b) O(logn), O(logn)
c) O(logn), O(n)
d) O(n), O(logn)
View Answer

Answer: d
Explanation: Worst case arises when the tree is skewed(either to the left or right) in which case
you have to process all the nodes of the tree giving O(n) complexity, otherwise O(logn) as you
process only the left half or the right half of the tree.

9. Given that 2 elements are present in the tree, write a function to find the LCA(Least Common
Ancestor) of the 2 elements.
a)

public void lca(Tree root,int n1, int n2)


{
while (root != NULL)
{
if (root.data() > n1 && root.data() > n2)
root = root.right();
else if (root.data() < n1 && root.data() < n2)
root = root.left();
else break;
}
System.out.println(root.data());
}

b)

public void lca(Tree root,int n1, int n2)


{
while (root != NULL)
{
if (root.data() > n1 && root.data() < n2)
root = root.left();
else if (root.data() < n1 && root.data() > n2)
root = root.right();
else break;
}
System.out.println(root.data());
}
c)

public void lca(Tree root,int n1, int n2)


{
while (root != NULL)
{
if (root.data() > n1 && root.data() > n2)
root = root.left();
else if (root.data() < n1 && root.data() < n2)
root = root.right();
else break;
}
System.out.println(root.data());
}

d)

public void lca(Tree root,int n1, int n2)


{
while (root != NULL)
{
if (root.data() > n1 && root.data() > n2)
root = root.left.left();
else if (root.data() < n1 && root.data() < n2)
root = root.right.right();
else break;
}
System.out.println(root.data());
}
View Answer
Answer: c
Explanation: The property of a binary search tree is that the lesser elements are to the left and
greater elements are to the right, we use this property here and iterate through the tree such that
we reach a point where the 2 elements are on 2 different sides of the node, this becomes the least
common ancestor of the 2 given elements.

10. What are the conditions for an optimal binary search tree and what is its advantage?
a) The tree should not be modified and you should know how often the keys are accessed, it
improves the lookup cost
b) You should know the frequency of access of the keys, improves the lookup time
c) The tree can be modified and you should know the number of elements in the tree before
hand, it improves the deletion time
d) The tree should be just modified and improves the lookup time
View Answer

Answer: a
Explanation: For an optimal binary search The tree should not be modified and we need to find
how often keys are accessed. Optimal binary search improves the lookup cost.
1. Advantages of linked list representation of binary trees over arrays?
a) dynamic size
b) ease of insertion/deletion
c) ease in randomly accessing a node
d) both dynamic size and ease in insertion/deletion
View Answer

Answer: d
Explanation: It has both dynamic size and ease in insertion and deletion as advantages.

2. Disadvantages of linked list representation of binary trees over arrays?


a) Randomly accessing is not possible
b) Extra memory for a pointer is needed with every element in the list
c) Difficulty in deletion
d) Random access is not possible and extra memory with every element
View Answer

Answer: d
Explanation: Random access is not possible with linked lists.

3. Which of the following traversing algorithm is not used to traverse in a tree?


a) Post order
b) Pre order
c) Post order
d) Randomized
View Answer

Answer: d
Explanation: Generally, all nodes in a tree are visited by using preorder, inorder and postorder
traversing algorithms.

4. Level order traversal of a tree is formed with the help of


a) breadth first search
b) depth first search
c) dijkstra’s algorithm
d) prims algorithm
View Answer

Answer: a
Explanation: Level order is similar to bfs.

5. Identify the reason which doesn’t play a key role to use threaded binary trees?
a) The storage required by stack and queue is more
b) The pointers in most of nodes of a binary tree are NULL
c) It is Difficult to find a successor node
d) They occupy less size
View Answer

Answer: d
Explanation: Threaded binary trees are introduced to make the Inorder traversal faster without
using any stack or recursion. Stack and Queue require more space and pointers in the majority of
binary trees are null and difficulties are raised while finding successor nodes. Size constraints are
not taken on threaded binary trees, but they occupy less space than a stack/queue.

6. The following lines talks about deleting a node in a binary tree.(the tree property must not be
violated after deletion)
i) from root search for the node to be deleted
ii)
iii) delete the node at
what must be statement ii) and fill up statement iii)
a) ii)-find random node,replace with node to be deleted. iii)- delete the node
b) ii)-find node to be deleted. iii)- delete the node at found location
c) ii)-find deepest node,replace with node to be deleted. iii)- delete a node
d) ii)-find deepest node,replace with node to be deleted. iii)- delete the deepest node
View Answer

Answer: d
Explanation: We just replace a to be deleted node with last leaf node of a tree. this must not be
done in case of BST or heaps.

7. What may be the psuedo code for finding the size of a tree?
a) find_size(root_node–>left_node) + 1 + find_size(root_node–>right_node)
b) find_size(root_node–>left_node) + find_size(root_node–>right_node)
c) find_size(root_node–>right_node) – 1
d) find_size(root_node–>left_node + 1
View Answer

Answer: a
Explanation: Draw a tree and analyze the expression. we are always taking size of left subtree
and right subtree and adding root value(1) to it and finally printing size.

8. What is missing in this logic of finding a path in the tree for a given sum (i.e checking whether
there will be a path from roots to leaf nodes with given sum)?

checkSum(struct bin-treenode *root , int sum) :


if(root==null)
return sum as 0
else :
leftover_sum=sum-root_node-->value
//missing
a) code for having recursive calls to either only left tree or right trees or to both subtrees
depending on their existence
b) code for having recursive calls to either only left tree or right trees
c) code for having recursive calls to either only left tree
d) code for having recursive calls to either only right trees
View Answer

Answer: a
Explanation: if(left subtree and right subtree) then move to both subtrees
else if only left subtree then move to left subtree carrying leftover_sum parameter
else if only right subtree then move to right subtree carrying leftover_sum parameter.

9. What must be the missing logic below so as to print mirror of a tree as below as an example?

if(rootnode):
mirror(rootnode-->left)
mirror(rootnode-->right)

//missing

end

a) swapping of left and right nodes is missing


b) swapping of left with root nodes is missing
c) swapping of right with root nodes is missing
d) nothing is missing
View Answer

Answer: a
Explanation: Mirror is another tree with left and right children of nodes are interchanged as
shown in the figure.

What is the code below trying to print?

void print(tree *root,tree *node)


{
if(root ==null) return 0
if(root-->left==node || root-->right==node || print(root-
>left,node)||printf(root->right,node)
{
print(root->data)
}
}

a) just printing all nodes


b) not a valid logic to do any task
c) printing ancestors of a node passed as argument
d) printing nodes from leaf node to a node passed as argument
View Answer

Answer: c
Explanation: We are checking if left or right node is what the argument sent or else if not the
case then move to left node or right node and print all nodes while searching for the argument
node.

You might also like