DSA MCQs
DSA MCQs
main()
{
char str[]="san foundry";
int len = strlen(str);
int i;
for(i=0;i<len;i++)
push(str[i]); // pushes an element into stack
for(i=0;i<len;i++)
pop(); //pops an element from the stack
}
a) yrdnuof nas
b) foundry nas
c) sanfoundry
d) san foundry
View Answer
Answer: a
Explanation: First, the string ‘san foundry’ is pushed one by one into the stack.
When it is popped, the output will be as ‘yrdnuof nas’.
24. Which of the following data structure can provide efficient searching of the
elements?
a) binary search tree
b) unordered lists
c) 2-3 tree
d) treap
View Answer
Answer: c
Explanation: The average case time for lookup in a binary search tree, treap
and 2-3 tree is O(log n) and in unordered lists it is O(n). But in the worst case,
only the 2-3 trees perform lookup efficiently as it takes O(log n), while others
take O(n).
25. What is an AVL tree?
a) a tree which is unbalanced and is a height balanced tree
b) a tree which is balanced and is a height balanced tree
c) a tree with atmost 3 children
d) a tree with three children
View Answer
Answer: b
Explanation: It is a self balancing tree with height difference atmost 1.
26. What is the time complexity for searching a key or integer in Van Emde
Boas data structure?
a) O (M!)
b) O (log M!)
c) O (log (log M))
d) O (M2)
View Answer
Answer: c
Explanation: In order to search a key or integer in the Van Emde Boas data
structure, the operation can be performed on an associative array. Hence, the
time complexity for searching a key or integer in Van Emde Boas data structure
is O (log (log M)).
27. The optimal data structure used to solve Tower of Hanoi is _________
a) Tree
b) Heap
c) Priority queue
d) Stack
View Answer
Answer: d
Explanation: The Tower of Hanoi involves moving of disks ‘stacked’ at one peg
to another peg with respect to the size constraint. It is conveniently done using
stacks and priority queues. Stack approach is widely used to solve Tower of
Hanoi.
28. What is the use of the bin data structure?
a) to have efficient traversal
b) to have efficient region query
c) to have efficient deletion
d) to have efficient insertion
View Answer
Answer: b
Explanation: Bin data structure allows us to have efficient region queries. A
frequency of bin is increased by one each time a data point falls into a bin.
29. Which is the most appropriate data structure for reversing a word?
a) stack
b) queue
c) graph
d) tree
View Answer
Answer: a
Explanation: Stack is the most appropriate data structure for reversing a word
because stack follows LIFO principle.
30. What is the functionality of the following piece of code?
Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
Pop();
Push(5);
struct node
{
int data;
struct node * next;
}
typedef struct node NODE;
NODE *ptr;
Which of the following c code is used to create new node?
a) ptr = (NODE*)malloc(sizeof(NODE));
b) ptr = (NODE*)malloc(NODE);
c) ptr = (NODE*)malloc(sizeof(NODE*));
d) ptr = (NODE)malloc(sizeof(NODE));
View Answer
Answer: a
Explanation: As it represents the right way to create a node.
1. What kind of linked list is best to answer questions like “What is the item at
position n?”
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Array implementation of linked list
View Answer
Answer: d
Explanation: Arrays provide random access to elements by providing the index
value within square brackets. In the linked list, we need to traverse through
each element until we reach the nth position. Time taken to access an element
represented in arrays is less than the singly, doubly and circular linked lists.
Thus, array implementation is used to access the item at the position n.
2. Linked lists are not suitable for the implementation of ___________
a) Insertion sort
b) Radix sort
c) Polynomial manipulation
d) Binary search
View Answer
Answer: d
Explanation: It cannot be implemented using linked lists.
3. Linked list is considered as an example of ___________ type of memory
allocation.
a) Dynamic
b) Static
c) Compile time
d) Heap
View Answer
Answer: a
Explanation: As memory is allocated at the run time.
advertisement
ADVERTISEMENT
ADVERTISEMENT
4. In Linked List implementation, a node carries information regarding
___________
a) Data
b) Link
c) Data and Link
d) Node
View Answer
Answer: c
Explanation: A linked list is a collection of objects linked together by references
from an object to another object. By convention these objects are names as
nodes. Linked list consists of nodes where each node contains one or more
data fields and a reference(link) to the next node.
5. Linked list data structure offers considerable saving in _____________
a) Computational Time
b) Space Utilization
c) Space Utilization and Computational Time
d) Speed Utilization
View Answer
Answer: c
Explanation: Linked lists saves both space and time.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
6. Which of the following points is/are not true about Linked List data structure
when it is compared with an array?
a) Arrays have better cache locality that can make them better in terms of
performance
b) It is easy to insert and delete elements in Linked List
c) Random access is not allowed in a typical implementation of Linked Lists
d) Access of elements in linked list takes less time than compared to arrays
View Answer
Answer: d
Explanation: To access an element in a linked list, we need to traverse every
element until we reach the desired element. This will take more time than
arrays as arrays provide random access to its elements.
7. What does the following function do for a given Linked List with first node as
head?
1->2->3->4->5->6
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
a) 1 4 6 6 4 1
b) 1 3 5 1 3 5
c) 1 2 3 5
d) 1 3 5 5 3 1
View Answer
Answer: d
Explanation: fun() prints alternate nodes of the given Linked List, first from
head to end, and then from end to head.
If Linked List has even number of nodes, then skips the last node.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
3. The following C function takes a simply-linked list as an input argument. It
modifies the list by moving the last element to the front of the list and returns
the modified list. Some part of the code is left blank. Choose the correct
alternative to replace the blank line.
struct node
{
int value;
struct node *next;
};
void rearrange(struct node *list)
{
struct node *p, * q;
int temp;
if ((!list) || !list->next)
return;
p = list;
q = list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
}
a) 1, 2, 3, 4, 5, 6, 7
b) 2, 1, 4, 3, 6, 5, 7
c) 1, 3, 2, 5, 4, 7, 6
d) 2, 3, 4, 5, 6, 7, 1
View Answer
Answer: b
Explanation: The function rearrange() exchanges data of every node with its
next node. It starts exchanging data from the first node itself.
5. In the worst case, the number of comparisons needed to search a singly
linked list of length n for a given element is?
a) log 2 n
b) n⁄2
c) log 2 n – 1
d) n
View Answer
Answer: d
Explanation: In the worst case, the element to be searched has to be compared
with all elements of the linked list.
6. Given pointer to a node X in a singly linked list. Only one pointer is given,
pointer to head node is not given, can we delete the node X from given linked
list?
a) Possible if X is not last node
b) Possible if size of linked list is even
c) Possible if size of linked list is odd
d) Possible if X is not first node
View Answer
Answer: a
Explanation: Following are simple steps.
struct node *temp = X->next;
X->data = temp->data;
X->next = temp->next;
free(temp);
7. You are given pointers to first and last nodes of a singly linked list, which of
the following operations are dependent on the length of the linked list?
a) Delete the first element
b) Insert a new element as a first element
c) Delete the last element of the list
d) Add a new element at the end of the list
View Answer
Answer: c
Explanation: Deletion of the first element of the list is done in O (1) time by
deleting memory and changing the first pointer.
Insertion of an element as a first element can be done in O (1) time. We will
create a node that holds data and points to the head of the given linked list.
The head pointer was changed to a newly created node.
Deletion of the last element requires a pointer to the previous node of last,
which can only be obtained by traversing the list. This requires the length of
the linked list.
Adding a new element at the end of the list can be done in O (1) by changing
the pointer of the last node to the newly created node and last is changed to a
newly created node.
8. In the worst case, the number of comparisons needed to search a singly
linked list of length n for a given element is?
a) log2 n
b) n⁄2
c) log2 n – 1
d) n
View Answer
Answer: d
Explanation: The worst-case happens if the required element is at last or the
element is absent in the list. For this, we need to compare every element in the
linked list. If n elements are there, n comparisons will happen in the worst
case.
1. Which of the following is not a disadvantage to the usage of array?
a) Fixed size
b) There are chances of wastage of memory space if elements inserted in an
array are lesser than the allocated size
c) Insertion based on position
d) Accessing elements at specified positions
View Answer
Answer: d
Explanation: Array elements can be accessed in two steps. First, multiply the
size of the data type with the specified position, second, add this value to the
base address. Both of these operations can be done in constant time, hence
accessing elements at a given index/position is faster.
2. What is the time complexity of inserting at the end in dynamic arrays?
a) O(1)
b) O(n)
c) O(logn)
d) Either O(1) or O(n)
View Answer
Answer: d
Explanation: Depending on whether the array is full or not, the complexity in
dynamic array varies. If you try to insert into an array that is not full, then the
element is simply stored at the end, this takes O(1) time. If you try to insert
into an array which is full, first you will have to allocate an array with double
the size of the current array and then copy all the elements into it and finally
insert the new element, this takes O(n) time.
3. What is the time complexity to count the number of elements in the linked
list?
a) O(1)
b) O(n)
c) O(logn)
d) O(n2)
View Answer
Answer: b
Explanation: To count the number of elements, you have to traverse through
the entire list, hence complexity is O(n).
advertisement
4. Which of the following performs deletion of the last element in the list?
Given below is the Node class.
class Node
{
protected Node next;
protected Object ele;
Node(Object e,Node n)
{
ele = e;
next = n;
}
public void setNext(Node n)
{
next = n;
}
public void setEle(Object e)
{
ele = e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class SLL
{
Node head;
int size;
SLL()
{
size = 0;
}
}
a)
class Node
{
protected Node next;
protected Object ele;
Node(Object e,Node n)
{
ele = e;
next = n;
}
public void setNext(Node n)
{
next = n;
}
public void setEle(Object e)
{
ele = e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class SLL
{
Node head;
int size;
SLL()
{
size = 0;
}
}
a)
#define MAX 10
public Stack()
{
stk = new Object[CAPACITY];
}
advertisement
class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class Stack
{
Node first;
int size=0;
Stack()
{
first=null;
}
}
a)
class Node
{
protected Node next;
protected Object ele;
Node()
{
this(null,null);
}
Node(Object e,Node n)
{
ele=e;
next=n;
}
public void setNext(Node n)
{
next=n;
}
public void setEle(Object e)
{
ele=e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class Stack
{
Node first;
int size=0;
Stack()
{
first=null;
}
}
a)
public void push(Object item)
{
Node temp = new Node(item,first);
first = temp;
size++;
}
b)
push(20);
push(4);
top();
pop();
pop();
push(5);
top();
a) 20
b) 4
c) stack underflow
d) 5
View Answer
Answer: d
Explanation: 20 and 4 which were pushed are popped by the two pop()
statements, the recent push() is 5, hence top() returns 5.
9. Which of the following data structures can be used for parentheses
matching?
a) n-ary tree
b) queue
c) priority queue
d) stack
View Answer
Answer: d
Explanation: For every opening brace, push it into the stack, and for every
closing brace, pop it off the stack. Do not take action for any other character.
In the end, if the stack is empty, then the input has balanced parentheses.
10. Minimum number of queues to implement stack is ___________
a) 3
b) 4
c) 1
d) 2
View Answer
Answer: c
Explanation: Use one queue and one counter to count the number of elements
in the queue.
1. Which of the following properties is associated with a queue?
a) First In Last Out
b) First In First Out
c) Last In First Out
d) Last In Last Out
View Answer
Answer: b
Explanation: Queue follows First In First Out structure.
2. In a circular queue, how do you increment the rear end of the queue?
a) rear++
b) (rear+1) % CAPACITY
c) (rear % CAPACITY)+1
d) rear–
View Answer
Answer: b
Explanation: Ensures rear takes the values from 0 to (CAPACITY-1).
3. What is the term for inserting into a full queue known as?
a) overflow
b) underflow
c) null pointer exception
d) program won’t be compiled
View Answer
Answer: a
Explanation: Just as stack, inserting into a full queue is termed overflow.
advertisement
4. What is the time complexity of enqueue operation?
a) O(logn)
b) O(nlogn)
c) O(n)
d) O(1)
View Answer
Answer: d
Explanation: Enqueue operation is at the rear end, it takes O(1) time to insert a
new item into the queue.
5. What does the following Java code do?
public CircularQueue()
{
this(CAPACITY);
}
public CircularQueue (int n)
{
size = n;
front = 0;
rear = 0;
q = new Object[size];
}
advertisement
ADVERTISEMENT
ADVERTISEMENT
public void insert_key(int key,Object item)
{
if(key<0)
{
Systerm.our.println("invalid");
System.exit(0);
}
else
{
Node temp = new Node(key,item,null);
if(count == 0)
{
head.setNext(temp);
temp.setNext(trail);
}
else
{
Node dup = head.getNext();
Node cur = head;
while((key>dup.getKey()) && (dup!=trail))
{
dup = dup.getNext();
cur = cur.getNext();
}
cur.setNext(temp);
temp.setNext(dup);
}
count++;
}
}
b)
advertisement
public void function(Object item)
{
Node temp = new Node(item,null);
if(isEmpty())
{
temp.setNext(trail);
head.setNext(trail);
}
else
{
Node cur = head.getNext();
temp.setNext(cur);
head.setNext(temp);
}
size++;
}
c)
6. Which of the following can be used to delete an element from the rear end
of the queue?
a)
public Object deleteRear() throws emptyDEQException
{
if(isEmpty())
throw new emptyDEQException("Empty");
else
{
Node temp = head.getNext();
Node cur = temp;
while(temp.getNext() != trail)
{
temp = temp.getNext();
cur = cur.getNext();
}
Object e = temp.getEle();
cur.setNext(trail);
size--;
return e;
}
}
b)
7. What is the time complexity of deleting from the rear end of the dequeue
implemented with a singly linked list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer
Answer: c
Explanation: Since a singly linked list is used, first you have to traverse till the
end, so the complexity is O(n).
8. After performing these set of operations, what does the final list look
contain?
InsertFront(10);
InsertFront(20);
InsertRear(30);
DeleteFront();
InsertRear(40);
InsertRear(10);
DeleteRear();
InsertRear(15);
display();
a) 10 30 10 15
b) 20 30 40 15
c) 20 30 40 10
d) 10 30 40 15
View Answer
Answer: d
Explanation: A careful tracing of the given operation yields the result.
10
20 10
20 10 30
10 30
10 30 40
10 30 40 10
10 30 40
10 30 40 15
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.
advertisement
ADVERTISEMENT
ADVERTISEMENT
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).
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
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
Answer: b
Explanation: Number of Leaf nodes in full binary tree is equal to 1 + Number of
Internal Nodes i.e L = I + 1
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)).
11. Construct a binary tree by using postorder and inorder sequences given
below.
Inorder: N, M, P, O, Q
Postorder: N, P, Q, O, M
a)
b)
c)
d)
View Answer
Answer: d
Explanation: Here,
Postorder Traversal: N, P, Q, O, M
Inorder Traversal: N, M, P, O, Q
Root node of tree is the last visiting node in Postorder traversal. Thus, Root
Node = ‘M’.
The partial tree constructed is:
The second last node in postorder traversal is O. Thus, node P becomes left
child of node O and node Q becomes right child of node Q. Thus, the final tree
is:
12. Construct a binary search tree by using postorder sequence given below.
Postorder: 2, 4, 3, 7, 9, 8, 5.
a)
b)
c)
d)
View Answer
Answer: b
Explanation: Postorder sequence is 2, 4, 3, 7, 9, 8, 5.
Inorder sequence is the ascending order of nodes in Binary search tree. Thus,
Inorder sequence is 2, 3, 4, 5, 7, 8, 9. The tree constructed using Postorder and
Inorder sequence is
13. Construct a binary tree using inorder and level order traversal given below.
Inorder Traversal: 3, 4, 2, 1, 5, 8, 9
Level Order Traversal: 1, 4, 5, 9, 8, 2, 3
a)
b)
c)
d)
View Answer
Answer: a
Explanation: Inorder Traversal: 3, 4, 2, 1, 5, 8, 9
Level Order Traversal: 1, 4, 5, 9, 8, 2, 3
In level order traversal first node is the root node of the binary tree.
Thus the partially formed tree is:
In level order traversal, the second node is 4. Then, node 3 becomes left child
of node 4 and node 2 becomes right child of node 4. Third node of level order
traversal is 8. Then, node 5 becomes left child of node 8 and node 9 becomes
right child of node 8. Thus, the final tree is:
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?
7. How will you find the maximum element in a binary search tree?
a)
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)
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.
11. Construct a binary search tree with the below information.
The preorder traversal of a binary search tree 10, 4, 3, 5, 11, 12.
a)
b)
c)
d)
View Answer
Answer: c
Explanation: Preorder Traversal is 10, 4, 3, 5, 11, 12. Inorder Traversal of Binary
search tree is equal to ascending order of the nodes of the Tree. Inorder
Traversal is 3, 4, 5, 10, 11, 12. The tree constructed using Preorder and Inorder
traversal is
1. What will be the height of a balanced full binary tree with 8 leaves?
a) 8
b) 5
c) 6
d) 4
View Answer
Answer: d
Explanation: A balanced full binary tree with l leaves has height h, where h =
log2l + 1.
So, the height of a balanced full binary tree with 8 leaves = log28 + 1 = 3 + 1 =
4.
2. The balance factor of a node in a binary tree is defined as _____
a) addition of heights of left and right subtrees
b) height of right subtree minus height of left subtree
c) height of left subtree minus height of right subtree
d) height of right subtree minus one
View Answer
Answer: c
Explanation: For a node in a binary tree, the difference between the heights of
its left subtree and right subtree is known as balance factor of the node.
3. Figure below is a balanced binary tree. If a node inserted as child of the node
R, how many nodes will become unbalanced?
a) 2
b) 1
c) 3
d) 0
View Answer
Answer: b
Explanation: Only the node P will become unbalanced, with balance factor +2.
advertisement
4. A binary tree is balanced if the difference between left and right subtree of
every node is not more than ____
a) 1
b) 3
c) 2
d) 0
View Answer
Answer: a
Explanation: In a balanced binary tree the heights of two subtrees of every
node never differ by more than 1.
5. Which of the following tree data structures is not a balanced binary tree?
a) AVL tree
b) Red-black tree
c) Splay tree
d) B-tree
View Answer
Answer: d
Explanation: All the tree data structures given in options are balanced, but B-
tree can have more than two children.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.
Participate Now!
6. Which of following figures is a balanced binary tree?
a)
b)
c)
d)
View Answer
Answer: b
Explanation: In Some tree diagrams, the root of tree has balance factor +2, so
the tree is not balanced. If every node in the tree is balanced, then it’s a
balanced tree.
7. Balanced binary tree with n items allows the lookup of an item in ____
worst-case time.
a) O(log n)
b) O(nlog 2)
c) O(n)
d) O(1)
View Answer
Answer: a
Explanation: Searching an item in balanced binary is fast and worst-case time
complexity of the search is O(log n).
8. Which of the following data structures can be efficiently implemented using
height balanced binary search tree?
a) sets
b) priority queue
c) heap
d) both sets and priority queue
View Answer
Answer: d
Explanation: Height-Balanced binary search tree can provide an efficient
implementation of sets, priority queues.
9. Two balanced binary trees are given with m and n elements respectively.
They can be merged into a balanced binary search tree in ____ time.
a) O(m+n)
b) O(mn)
c) O(m)
d) O(mlog n)
View Answer
Answer: a
Explanation: First we store the in-order traversals of both the trees in two
separate arrays and then we can merge these sorted sequences in O(m+n)
time. And then we construct the balanced tree from this final sorted array.
10. Which of the following is an advantage of balanced binary search tree, like
AVL tree, compared to binary heap?
a) insertion takes less time
b) deletion takes less time
c) searching takes less time
d) construction of the tree takes less time than binary heap
View Answer
Answer: a
Explanation: Insertion and deletion, in both the binary heap and balanced
binary search tree takes O(log n). But searching in balanced binary search tree
requires O(log n) while binary heap takes O(n). Construction of balanced binary
search tree takes O(nlog n) time while binary heap takes O(n).
11. AVL trees are more balanced than Red-black trees.
a) True
b) False
View Answer
Answer: a
Explanation: AVL tree is more balanced than a Red-black tree because AVL tree
has less height than Red-black tree given that both trees have the same
number of elements.
12. The figure shown below is a balanced binary tree. If node P is deleted,
which of the following nodes will get unbalanced?
a) U
b) M
c) H
d) A
View Answer
Answer: a
Explanation: Node U will get unbalanced if node P is deleted, because it’s
balance factor will become -2.