Data Structure
Data Structure
Questions)
Here are 1000 Data Structure MCQ (Chapterwise).
1. What is a data structure?
a) A programming language
b) A collection of algorithms
c) A way to store and organize data
d) A type of computer hardware
View Answer
Answer: c
Explanation: A data structure is a way to store and organize data efficiently, enhancing
access and manipulation, unlike programming languages, algorithms, or computer
hardware.
8. What data structure would you mostly likely see in non recursive implementation of a
recursive algorithm?
a) Stack
b) Linked List
c) Tree
d) Queue
View Answer
Answer: a
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.
9. Which of the following statement(s) about stack data structure is/are NOT correct?
a) Top of the Stack always contain the new node
b) Stack is the FIFO data structure
c) Null link is present in the last node at the bottom of the stack
d) Linked List are used for implementing Stacks
View Answer
Answer: b
Explanation: Stack follows LIFO.
10. The data structure required for Breadth First Traversal on a graph is?
a) Array
b) Stack
c) Tree
d) Queue
View Answer
Answer: d
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 the first unvisited vertex we need to follows First In First Out principle.
Queue uses FIFO principle.
12. Which of the following points is/are not true about Linked List data structure when it is
compared with an array?
a) Random access is not allowed in a typical implementation of Linked Lists
b) Access of elements in linked list takes less time than compared to arrays
c) Arrays have better cache locality that can make them better in terms of performance
d) It is easy to insert and delete elements in Linked List
View Answer
Answer: b
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.
13. Which data structure is based on the Last In First Out (LIFO) principle?
a) Tree
b) Linked List
c) Stack
d) Queue
View Answer
Answer: c
Explanation: The data structure that follows the Last In First Out (LIFO) principle is the
Stack. It operates like a stack of objects, making it suitable for specific-order management.
14. Which of the following application makes use of a circular linked list?
a) Recursive function calls
b) Undo operation in a text editor
c) Implement Hash Tables
d) Allocating CPU to resources
View Answer
Answer: d
Explanation: Generally, round robin fashion is employed to allocate CPU time to resources
which makes use of the circular linked list data structure. Recursive function calls use stack
data structure. Undo Operation in text editor uses doubly linked lists. Hash tables uses
singly linked lists.
16. Which of the following tree data structures is not a balanced binary tree?
a) Splay tree
b) B-tree
c) AVL tree
d) Red-black tree
View Answer
Answer: b
Explanation: All the tree data structures given in options are balanced, but B-tree can have
more than two children.
18. 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.
21. Which of the following is the most widely used external memory data structure?
a) B-tree
b) Red-black tree
c) AVL tree
d) Both AVL tree and Red-black tree
View Answer
Answer: a
Explanation: In external memory, the data is transferred in form of blocks. These blocks
have data valued and pointers. And B-tree can hold both the data values and pointers. So
B-tree is used as an external memory data structure.
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).
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.
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.
31. Which of the following is the simplest data structure that supports range searching?
a) AA-trees
b) K-d trees
c) Heaps
d) binary search trees
View Answer
Answer: c
Explanation: K-d trees are the simplest data structure that supports range searching and
also it achieves the respectable running time.
35. A data structure in which elements can be inserted or deleted at/from both ends but not
in the middle is?
a) Priority queue
b) Dequeue
c) Circular queue
d) Queue
View Answer
Answer: b
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.
a) 4 and 2
b) 2 and 4
c) 5 and 3
d) 3 and 5
View Answer
Answer: d
Explanation: Array indexing starts from 0.
a) 3 and 5
b) 5 and 3
c) 2 and 4
d) 4 and 2
View Answer
Answer: a
Explanation: Array indexing starts from 0.
a) 4
b) 5
c) ArrayIndexOutOfBoundsException
d) InavlidInputException
View Answer
Answer: c
Explanation: Trying to access an element beyond the limits of an array gives
ArrayIndexOutOfBoundsException.
a) 3 and 5
b) 5 and 3
c) 2 and 4
d) 4 and 2
View Answer
Answer: a
Explanation: Array indexing starts from 0.
a) 4
b) 5
c) ArrayIndexOutOfBoundsException
d) InavlidInputException
View Answer
Answer: c
Explanation: Trying to access an element beyond the limits of an array gives
ArrayIndexOutOfBoundsException.
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.
Data Structure Questions and Answers –
Stack Operations – 2
This set of Data Structure Interview Questions and Answers focuses on “Stack Operations –
2”.
3. What data structure would you most likely see in 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.
advertisement
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.
a) (A B D ⋀ + E F – / G +)
c) (A B D ⋀ + E F/- G +)
b) (A B D +⋀ E F – / G +)
d) (A B D E F + ⋀ / – G +)
View Answer
(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 +.
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 is?
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.
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. And X is right associative. Thus
the final postfix expression is abc X+def^^-
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.
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) 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 the first unvisited vertex we need to follows First In First Out principle.
Queue uses FIFO principle.
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 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.
1. A linear collection of data elements where the linear node is given by means of pointer is
called?
a) Linked list
b) Node list
c) Primitive list
d) Unordered list
View Answer
Answer: a
Explanation: In Linked list each node has its own data and the address of next node. These
nodes are linked by using pointers. Node list is an object that consists of a list of all nodes in
a document with in a particular selected set of nodes.
2. Consider an implementation of unsorted singly linked list. Suppose it has its
representation with a head pointer only. Given the representation, which of the following
operation can be implemented in O(1) time?
a) I and II
b) I and III
c) I, II and III
d) I, II and IV
View Answer
Answer: b
Explanation: We know the head node in the given linked list. Insertion and deletion of
elements at the front of the linked list completes in O (1) time whereas for insertion and
deletion at the last node requires to traverse through every node in the linked list. Suppose
there are n elements in a linked list, we need to traverse through each node. Hence time
complexity becomes O(n).
advertisement
ADVERTISEMENT
ADVERTISEMENT
3. In linked list each node contains a minimum of two fields. One field is data field to store
the data second field is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
View Answer
Answer: c
Explanation: Each node in a linked list contains data and a pointer (reference) to the next
node. Second field contains pointer to node.
4. What would be the asymptotic time complexity to add a node at the end of singly linked
list, if the pointer is initially pointing to the head of the list?
a) O(1)
b) O(n)
c) θ(n)
d) θ(1)
View Answer
Answer: c
Explanation: In case of a linked list having n elements, we need to travel through every
node of the list to add the element at the end of the list. Thus asymptotic time complexity is
θ(n).
5. What would be the asymptotic time complexity to insert an element at the front of the
linked list (head is known)?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Answer: a
Explanation: To add an element at the front of the linked list, we will create a new node
which holds the data to be added to the linked list and pointer which points to head position
in the linked list. The entire thing happens within O (1) time. Thus the asymptotic time
complexity is O (1).
6. What would be the asymptotic time complexity to find an element in the linked list?
a) O(1)
b) O(n)
c) O(n2)
d) O(n4)
View Answer
Answer: b
Explanation: If the required element is in the last position, we need to traverse the entire
linked list. This will take O (n) time to search the element.
7. What would be the asymptotic time complexity to insert an element at the second position
in the linked list?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Answer: a
Explanation: A new node is created with the required element. The pointer of the new node
points the node to which the head node of the linked list is also pointing. The head node
pointer is changed and it points to the new node which we created earlier. The entire
process completes in O (1) time. Thus the asymptotic time complexity to insert an element
in the second position of the linked list is O (1).
8. The concatenation of two lists can be performed in O(1) time. Which of the following
variation of the linked list can be used?
a) Singly linked list
b) Doubly linked list
c) Circular doubly linked list
d) Array implementation of list
View Answer
Answer: c
Explanation: We can easily concatenate two lists in O (1) time using singly or doubly linked
list, provided that we have a pointer to the last node at least one of the lists. But in case of
circular doubly linked lists, we will break the link in both the lists and hook them together.
Thus circular doubly linked list concatenates two lists in O (1) time.
9. Consider the following definition in c programming language.
struct node
{
int data;
struct node * next;
}
typedef struct node NODE;
NODE *ptr;
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.
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?
8. Which of the following sorting algorithms can be used to sort a random linked list with
minimum time complexity?
a) Insertion Sort
b) Quick Sort
c) Heap Sort
d) Merge Sort
View Answer
Answer: d
Explanation: Both Merge sort and Insertion sort can be used for linked lists. The slow
random-access performance of a linked list makes other algorithms (such as quicksort)
perform poorly, and others (such as heapsort) completely impossible. Since worst case time
complexity of Merge Sort is O(nLogn) and Insertion sort is O(n2), merge sort is preferred
1. The following function reverse() is supposed to reverse a singly linked list. There is one
line missing at the end of the function.
What should be added in place of “/*ADD A STATEMENT HERE*/”, so that the function
correctly reverses a linked list.
a) *head_ref = prev;
b) *head_ref = current;
c) *head_ref = next;
d) *head_ref = NULL;
View Answer
Answer: a
Explanation: *head_ref = prev; At the end of while loop, the prev pointer points to the last
node of original linked list.
We need to change *head_ref so that the head pointer now starts pointing to the last node.
advertisement
ADVERTISEMENT
ADVERTISEMENT
2. What is the output of following function for start pointing to first node of following linked
list?
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.
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.
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
ADVERTISEMENT
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)
b)
c)
d)
View Answer
Answer: a
Explanation: Since you have to traverse to the end of the list and delete the last node, you
need two reference pointers. ‘cur’ to traverse all the way and find the last node, and ‘temp’
is a trailing pointer to ‘cur’. Once you reach the end of the list, setNext of ‘temp’ to null, ‘cur’
is not being pointed to by any node, and hence it is available for garbage collection.
7. How would you delete a node in the singly linked list? The position to be deleted is given.
a)
b)
c)
d)
View Answer
Answer: a
Explanation: Loop through the list to get into position one behind the actual position given.
temp.setNext(temp.getNext().getNext()) will delete the specified node.
9. Which of the following piece of code has the functionality of counting the number of
elements in the list?
a)
b)
c)
d)
View Answer
Answer: a
Explanation: ‘cur’ pointer traverses through list and increments the size variable until the
end of list is reached.
b)
d)
View Answer
Answer: a
Explanation: Set the ‘next’ pointer point to the head of the list and then make this new node
as the head.
2. Given the Node class implementation, select one of the following that correctly inserts a
node at the tail of the list.
a)
advertisement
ADVERTISEMENT
ADVERTISEMENT
public void insertRear(int data)
{
Node node = new Node(data,tail.getPrev(),tail);
node.getPrev().setNext(node);
tail.setPrev(node);
length++;
}
b)
c)
public void insertRear(int data)
{
Node node = new Node(data,tail.getPrev(),tail);
node.getPrev().setNext(tail);
tail.setPrev(node);
length++;
}
d)
View Answer
Answer: a
Explanation: First create a new node whose ‘prev’ points to the node pointed to by the ‘prev’
of tail. The ‘next’ of the new node should point to tail. Set the ‘prev’ of tail to point to new
node and the ‘prev’ of new node to point to the new node.
4. Which of the following piece of code removes the node from a given position?
a)
b)
c)
d)
View Answer
Answer: a
Explanation: If the position to be deleted is not the head, advance to the given position and
manipulate the previous and next pointers of next and previous nodes respectively.
5. How do you calculate the pointer difference in a memory efficient double linked list?
a) head xor tail
b) pointer to previous node xor pointer to next node
c) pointer to previous node – pointer to next node
d) pointer to next node – pointer to previous node
View Answer
Answer: b
Explanation: The pointer difference is calculated by taking XOR of pointer to previous node
and pointer to the next node.
6. What is the worst case time complexity of inserting a node in a doubly linked list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
View Answer
Answer: c
Explanation: In the worst case, the position to be inserted maybe at the end of the list,
hence you have to traverse through the entire list to get to the correct position, hence O(n).
b)
c)
d)
View Answer
Answer: a
Explanation: The new node’s previous pointer will point to head and next pointer will point to
the current next of head.
8. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list after
performing the given sequence of operations?
a) head-0-1-2-3-4-5-6-tail
b) head-1-2-3-4-5-6-tail
c) head-6-1-2-3-4-5-0-tail
d) head-0-1-2-3-4-5-tail
View Answer
Answer: c
Explanation: The given sequence of operations performs addition of nodes at the head and
tail of the list.
10. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be the list after
performing the given sequence of operations?
a) head-6-1-2-3-4-5-tail
b) head-6-1-2-3-4-tail
c) head-1-2-3-4-5-6-tail
d) head-1-2-3-4-5-tail
View Answer
Answer: b
Explanation: A new node is added to the head of the list and a node is deleted from the tail
end of the list.
2. How do you count the number of elements in the circular linked list?
a)
b)
advertisement
public int length(Node head)
{
int length = 0;
if( head == null)
return 0;
Node temp = head.getNext();
while(temp != null)
{
temp = temp.getNext();
length++;
}
return length;
}
c)
d)
View Answer
Answer: a
Explanation: If the head is null, it means that the list is empty. Otherwise, traverse the list
until the head of the list is reached.
3. What is the functionality of the following piece of code? Select the most appropriate.
4. What is the time complexity of searching for an element in a circular linked list?
a) O(n)
b) O(nlogn)
c) O(1)
d) O(n2)
View Answer
Answer: a
Explanation: In the worst case, you have to traverse through the entire list of n elements.
5. Which of the following application makes use of a circular linked list?
a) Undo operation in a text editor
b) Recursive function calls
c) Allocating CPU to resources
d) Implement Hash Tables
View Answer
Answer: c
Explanation: Generally, round robin fashion is employed to allocate CPU time to resources
which makes use of the circular linked list data structure. Recursive function calls use stack
data structure. Undo Operation in text editor uses doubly linked lists. Hash tables uses
singly linked lists.
6. Choose the code snippet which inserts a node to the head of the list?
a)
b)
d)
View Answer
Answer: a
Explanation: If the list is empty make the new node as ‘head’, otherwise traverse the list to
the end and make its ‘next’ pointer point to the new node, set the new node’s next point to
the current head and make the new node as the head.
7. What is the functionality of the following code? Choose the most appropriate answer.
8. What is the functionality of the following code? Choose the most appropriate answer.
10. Consider a small circular linked list. How to detect the presence of cycles in this list
effectively?
a) Keep one node as head and traverse another temp node till the end to check if its ‘next
points to head
b) Have fast and slow pointers with the fast pointer advancing two nodes at a time and slow
pointer advancing by one node at a time
c) Cannot determine, you have to pre-define if the list contains cycles
d) Circular linked list itself represents a cycle. So no new cycles cannot be generated
View Answer
Answer: b
Explanation: Advance the pointers in such a way that the fast pointer advances two nodes
at a time and slow pointer advances one node at a time and check to see if at any given
instant of time if the fast pointer points to slow pointer or if the fast pointer’s ‘next’ points to
the slow pointer. This is applicable for smaller lists.
1. Which of the following real world scenarios would you associate with a stack data
structure?
a) piling up of chairs one above the other
b) people standing in a line to be serviced at a counter
c) offer services based on the priority of the customer
d) tatkal Ticket Booking in IRCTC
View Answer
Answer: a
Explanation: Stack follows Last In First Out (LIFO) policy. Piling up of chairs one above the
other is based on LIFO, people standing in a line is a queue and if the service is based on
priority, then it can be associated with a priority queue. Tatkal Ticket Booking Follows First
in First Out Policy. People who click the book now first will enter the booking page first.
2. What does the following function check for? (all necessary headers to be included and
function is called from main)
#define MAX 10
a) full stack
b) invalid index
c) empty stack
d) infinite stack
View Answer
Answer: c
Explanation: An empty stack is represented with the top-of-the-stack(‘top’ in this case) to be
equal to -1.
advertisement
ADVERTISEMENT
ADVERTISEMENT
public Stack()
{
stk = new Object[CAPACITY];
}
public void push(Object item)
{
if(size_of_stack==size)
{
System.out.println("Stack overflow");
return;
}
else
{
top++;
stk[top]=item;
}
}
public Object pop()
{
if(top<0)
{
return -999;
}
else
{
Object ele=stk[top];
top--;
size_of_stack--;
return ele;
}
}
}
a) stack is full
b) 20
c) 0
d) -999
View Answer
Answer: d
Explanation: The first call to pop() returns 10, whereas the second call to pop() would result
in stack underflow and the program returns -999.
5. What is the time complexity of pop() operation when the stack is implemented using an
array?
a) O(1)
b) O(n)
c) O(logn)
d) O(nlogn)
View Answer
Answer: a
Explanation: pop() accesses only one end of the structure, and hence constant time.
6. Which of the following array position will be occupied by a new element being pushed for
a stack of size N elements(capacity of stack > N)?
a) S[N-1]
b) S[N]
c) S[1]
d) S[0]
View Answer
Answer: b
Explanation: Elements are pushed at the end, hence N.
7. What happens when you pop from an empty stack while implementing using the Stack
ADT in Java?
a) Undefined error
b) Compiler displays a warning
c) EmptyStackException is thrown
d) NoStackException is thrown
View Answer
Answer: c
Explanation: The Stack ADT throws an EmptyStackException if the stack is empty and a
pop() operation is tried on it.
9. Array implementation of Stack is not dynamic, which of the following statements supports
this argument?
a) space allocation for array is fixed and cannot be changed during run-time
b) user unable to give the input for stack operations
c) a runtime exception halts execution
d) improper program compilation
View Answer
Answer: a
Explanation: You cannot modify the size of an array once the memory has been allocated,
adding fewer elements than the array size would cause wastage of space, and adding more
elements than the array size at run time would cause Stack Overflow.
10. Which of the following array element will return the top-of-the-stack-element for a stack
of size N elements(capacity of stack > N)?
a) S[N-1]
b) S[N]
c) S[N-2]
d) S[N+1]
View Answer
Answer: a
Explanation: Array indexing start from 0, hence N-1 is the last index.
1. What is the best case time complexity of deleting a node in a Singly Linked list?
a) O (n)
b) O (n2)
c) O (nlogn)
d) O (1)
View Answer
Answer: d
Explanation: Deletion of the head node in the linked list is taken as the best case. The
successor of the head node is changed to head and deletes the predecessor of the newly
assigned head node. This process completes in O(1) time.
2. Which of the following statements are not correct with respect to Singly Linked List(SLL)
and Doubly Linked List(DLL)?
a) Complexity of Insertion and Deletion at known position is O(n) in SLL and O(1) in DLL
b) SLL uses lesser memory per node than DLL
c) DLL has more searching power than SLL
d) Number of node fields in SLL is more than DLL
View Answer
Answer: d
Explanation: To insert and delete at known positions requires complete traversal of the list
in worst case in SLL, SLL consists of an item and a node field, while DLL has an item and
two node fields, hence SLL occupies lesser memory, DLL can be traversed both ways(left
and right), while SLL can traverse in only one direction, hence more searching power of
DLL. Node fields in SLL is 2 (data and address of next node) whereas in DLL is 3(data,
address to next node, address to previous node).
3. Given below is the Node class to perform basic list operations and a Stack class with a
no arg constructor.
Select from the options the appropriate pop() operation that can be included in the Stack
class. Also ‘first’ is the top-of-the-stack.
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)
b)
c)
View Answer
Answer: a
Explanation: pop() should return the Object pointed to by the node ‘first’. The sequence of
operations is, first, get the element stored at node ‘first’ using getEle(), and second, make
the node point to the next node using getNext().
a) pop
b) delete the top-of-the-stack element
c) retrieve the top-of-the-stack element
d) push operation
View Answer
Answer: c
Explanation: This code is only retrieving the top element, note that it is not equivalent to pop
operation as you are not setting the ‘next’ pointer point to the next node in sequence.
7. Given below is the Node class to perform basic list operations and a Stack class with a
no arg constructor. Select from the options the appropriate push() operation that can be
included in the Stack class. Also ‘first’ is the top-of-the-stack.
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)
b)
c)
d)
View Answer
Answer: a
Explanation: To push an element into the stack, first create a new node with the next pointer
point to the current top-of-the-stack node, then make this node as top-of-the-stack by
assigning it to ‘first’.
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.
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.
a) Dequeue
b) Enqueue
c) Return the front element
d) Return the last element
View Answer
Answer: c
Explanation: q[front] gives the element at the front of the queue, since we are not moving
the ‘front’ to the next element,
it is not a dequeue operation.
7. Which of the following represents a dequeue operation? (count is the number of elements
in the queue)
a)
b)
c)
d)
View Answer
Answer: a
Explanation: Dequeue removes the first element from the queue, ‘front’ points to the front
end of the queue and returns the first element.
8. Which of the following best describes the growth of a linear queue at runtime? (Q is the
original queue, size() returns the number of elements in the queue)
a)
b)
c)
d)
View Answer
Answer: a
Explanation: A common technique to expand the size of array at run time is simply to
double the size. Create a new array of double the previous size and copy all the elements,
after copying do not forget to assign front = 0 and rear = size()-1, as these are necessary to
maintain the decorum of the queue operations.
public CircularQueue()
{
this(CAPACITY);
}
public CircularQueue (int n)
{
size = n;
front = 0;
rear = 0;
q = new Object[size];
}
a) 3 3
b) 3 6
c) 6 6
d) 10 6
View Answer
Answer: a
Explanation: First enqueue 10 and 3 into the queue, followed by a dequeue(removes 10),
followed by an enqueue(6), At this point, 3 is at the front end of the queue and 6 at the rear
end, hence a call to frontElement() will return 3 which is displayed twice.
1. In linked list implementation of queue, if only front pointer is maintained, which of the
following operation take worst case linear time?
a) Insertion
b) Deletion
c) To empty a queue
d) Both Insertion and To empty a queue
View Answer
Answer: d
Explanation: Since front pointer is used for deletion, so worst time for the other two cases.
3. In linked list implementation of a queue, front and rear pointers are tracked. Which of
these pointers will change during an insertion into a NONEMPTY queue?
a) Only front pointer
b) Only rear pointer
c) Both front and rear pointer
d) No pointer will be changed
View Answer
Answer: b
Explanation: Since queue follows FIFO so new element inserted at last.
advertisement
ADVERTISEMENT
ADVERTISEMENT
4. In linked list implementation of a queue, front and rear pointers are tracked. Which of
these pointers will change during an insertion into EMPTY queue?
a) Only front pointer
b) Only rear pointer
c) Both front and rear pointer
d) No pointer will be changed
View Answer
Answer: c
Explanation: Since its the starting of queue, so both values are changed.
5. In case of insertion into a linked queue, a node borrowed from the __________ list is
inserted in the queue.
a) AVAIL
b) FRONT
c) REAR
d) NULL
View Answer
Answer: a
Explanation: All the nodes are collected in AVAIL list.
8. The essential condition which is checked before insertion in a linked queue is?
a) Underflow
b) Overflow
c) Front value
d) Rear value
View Answer
Answer: b
Explanation: To check whether there is space in the queue or not.
9. The essential condition which is checked before deletion in a linked queue is?
a) Underflow
b) Overflow
c) Front value
d) Rear value
View Answer
Answer: a
Explanation: To check whether there is element in the list or not.
10. Which of the following is true about linked list implementation of queue?
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 beginning, then in pop operation,
nodes must be removed from the beginning
c) In push operation, if new nodes are inserted at the end, then in pop operation, nodes
must be removed from end
d) In push operation, if new nodes are inserted at the end, then in pop operation, nodes
must be removed from beginning
View Answer
Answer: a
Explanation: It can be done by both the methods.
3. Select the appropriate code that inserts elements into the list based on the given key
value.
(head and trail are dummy nodes to mark the end and beginning of the list, they do not
contain any priority or element)
a)
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)
c)
d)
View Answer
Answer: a
Explanation: Have two temporary pointers ‘dup’ and ‘cur’ with ‘cur’ trailing behind ‘dup’.
Traverse through the list until the given key is greater than some element with a lesser key,
insert the new node ‘temp’ in that position.
4. What is the time complexity to insert a node based on key in a priority queue?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer
Answer: c
Explanation: In the worst case, you might have to traverse the entire list.
8. What is the time complexity to insert a node based on position in a priority queue?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer
Answer: c
Explanation: In the worst case, you might have to traverse the entire list.
1. What is a dequeue?
a) A queue with insert/delete defined for both front and rear ends of the queue
b) A queue implemented with a doubly linked list
c) A queue implemented with both singly and doubly linked lists
d) A queue with insert/delete defined for front side of the queue
View Answer
Answer: a
Explanation: A dequeue or a double ended queue is a queue with insert/delete defined for
both front and rear ends of the queue.
2. Select the function which performs insertion at the front end of the dequeue?
a)
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)
d)
public void function(Object item)
{
Node temp = new Node(item,null);
if(isEmpty())
{
Node cur = head.getNext();
temp.setNext(cur);
cur.setNext(temp);
}
else
{
head.setNext(trail);
trail.setNext(temp);
}
size++;
}
View Answer
Answer: a
Explanation: Create a new node, if the current list is empty, the ‘head’ points to this node
and this new node points to ‘trail’. Otherwise, ‘head’ points to the new node and this in turn
points to the current first element(head.getNext()).
5. Which of the following can be used to delete an element from the front end of the queue?
a)
b)
c)
d)
View Answer
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)
c)
d)
View Answer
Answer: c
Explanation: Traverse till the end of the list with a pointer ‘temp’ and another ‘cur’ which is
trailing behind temp, make ‘cur’ point to trail, this removes all reference for ‘temp’.
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
1. How many stacks are required for applying evaluation of infix expression algorithm?
a) one
b) two
c) three
d) four
View Answer
Answer: b
Explanation: Two stacks are required for evaluation of infix expression – one for operands
and one for operators.
2. How many passes does the evaluation of infix expression algorithm makes through the
input?
a) One
b) Two
c) Three
d) Four
View Answer
Answer: a
Explanation: Evaluation of infix expression algorithm is linear and makes only one pass
through the input.
3. Identify the infix expression from the list of options given below.
a) a/b+(c-d)
b) abc*+d+ab+cd+*ce-f-
c) ab-c-
d) +ab
View Answer
Answer: a
Explanation: a/b+(c-d) is an infix expression since the operators are placed in between the
operands.
advertisement
4. Which of the following statement is incorrect with respect to evaluation of infix expression
algorithm?
a) Operand is pushed on to the stack
b) If the precedence of operator is higher, pop two operands and evaluate
c) If the precedence of operator is lower, pop two operands and evaluate
d) The result is pushed on to the operand stack
View Answer
Answer: b
Explanation: If the precedence of the operator is higher than the stack operator, then it is
pushed on to the stack operator.
5. Evaluate the following statement using infix evaluation algorithm and choose the correct
answer. 1+2*3-2
a) 3
b) 6
c) 5
d) 4
View Answer
Answer: c
Explanation: According to precedence of operators, * is evaluated first. + and – have equal
priorities. Hence, 1+6-2= 5.
10. Evaluate the following statement using infix evaluation algorithm and choose the correct
answer. 4*2+3-5/5
a) 10
b) 11
c) 16
d) 12
View Answer
Answer: a
Explanation: 4*2 and 5/5 are evaluated first and then, 8+3-1 is evaluated and the result is
obtained as 10.
11. Using the evaluation of infix expression, evaluate a^b+c and choose the correct answer.
(a=2, b=2, c=2)
a) 12
b) 8
c) 10
d) 6
View Answer
Answer: d
Explanation: ^ has the highest precedence. Hence, 2^2 is evaluated and then 4+2 gives 6.
12. Evaluate the following infix expression using algorithm and choose the correct answer.
a+b*c-d/e^f where a=1, b=2, c=3, d=4, e=2, f=2.
a) 6
b) 8
c) 9
d) 7
View Answer
Answer: a
Explanation: ^ has the highest order of precedence. Hence, 2^2 is evaluated first, and then,
2*3 and 4/4 are evaluated. Therefore, 1+6-1=6.
13. From the given expression tree, identify the infix expression, evaluate it and choose the
correct result.
a) 5
b) 10
c) 12
d) 16
View Answer
Answer: c
Explanation: From the given expression tree, the result of the infix expression is evaluated
to be 12.
advertisement
a) 2
b) 12
c) 10
d) 4
View Answer
Answer: a
Explanation: The given prefix expression is evaluated using two stacks and the value is
given by (2+2-1)*(4-2)/(5-3+1)= 2.
10. In the given C snippet, find the statement number that has error.
advertisement
10. What is the result of the given postfix expression? abc*+ where a=1, b=2, c=3.
a) 4
b) 5
c) 6
d) 7
View Answer
Answer: d
Explanation: The infix expression is a+b*c. Evaluating it, we get 1+2*3=7.
13. Evaluate the postfix expression ab + cd/- where a=5, b=4, c=9, d=3.
a) 23
b) 15
c) 6
d) 10
View Answer
Answer: c
Explanation: The infix expression is (a+b)-c/d. Evaluating it, (5+4)-9/3 gives 6.
14. Evaluate and write the result for the following postfix expression
abc*+de*f+g*+ where a=1, b=2, c=3, d=4, e=5, f=6, g=2.
a) 61
b) 59
c) 60
d) 55
View Answer
Answer: b
Explanation: The infix expression is a+b*c+(d*e+f)*g. Evaluating it, 1+2*3+(4*5+6)*2 gives
59.
15. For the given expression tree, write the correct postfix expression.
a) abc*+
b) abc+*
c) ab+c*
d) a+bc*
View Answer
Answer: a
Explanation: Evaluating the given expression tree gives the infix expression a+b*c.
Converting it to postfix, we get, abc*+.
1. What data structure is used when converting an infix notation to prefix notation?
a) Stack
b) Queue
c) B-Trees
d) Linked-list
View Answer
Answer: a
Explanation: First you reverse the given equation and carry out the algorithm of infix to
postfix expression. Here, the data structure used is stacks.
A+(B*C)
a) +A*CB
b) *B+AC
c) +A*BC
d) *A+CB
View Answer
Answer: c
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-
postfix algorithm. The equation inside the bracket evaluates to CB* and outside the bracket
evaluates to A+ therefore getting CB*A+. Reversing this and we get +A*BC.
advertisement
A+B*C^D
a) +A*B^CD
b) +A^B*CD
c) *A+B^CD
d) ^A*B+CD
View Answer
Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-
prefix algorithm. The preference order in ascending order are as follows +*^. Operators are
pushed into the stack and popped if its preference is greater than the one which is getting
pushed. In the end all operators are popped. The equation evaluates to DC^B*A+.
Reversing this we get our following answer.
5. Out of the following operators (^, *, +, &, $), the one having highest priority is _________
a) +
b) $
c) ^
d) &
View Answer
Answer: c
Explanation: According to the algorithm (infix-prefix), it follows that the exponentiation will
have the highest priority.
6. Out of the following operators (|, *, +, &, $), the one having lowest priority is ________
a) +
b) $
c) |
d) &
View Answer
Answer: c
Explanation: According to the algorithm (infix-prefix), it follows that the logical OR will have
the lowest priority.
A^B^C^D
a) ^^^ABCD
b) ^A^B^CD
c) ABCD^^^
d) AB^C^D
View Answer
Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-
prefix algorithm. Here we have to remember that the exponentiation has order of
associativity from right to left. Therefore the stack goes on pushing ^. Therefore resulting in
^^^ABCD.
a+b-c/d&e|f
a) |&-+ab/cdef
b) &|-+ab/cdef
c) |&-ab+/cdef
d) |&-+/abcdef
View Answer
Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-
prefix algorithm. The preference order in ascending order are as follows |&+*/.
(a+(b/c)*(d^e)-f)
a) -+a*/^bcdef
b) -+a*/bc^def
c) -+a*b/c^def
d) -a+*/bc^def
View Answer
Answer: b
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-
prefix algorithm. The preference order in ascending order are as follows +*/^. Brackets have
the highest priority. The equations inside the brackets are solved first.
10. What would be the Prefix notation and Postfix notation for the given equation?
A+B+C
a) ++ABC and AB+C+
b) AB+C+ and ++ABC
c) ABC++ and AB+C+
d) ABC+ and ABC+
View Answer
Answer: a
Explanation: For prefix notation there is a need of reversing the giving equation and solving
it as a normal infix-postfix question. We see that it doesn’t result as same as normal infix-
postfix conversion.
11. What would be the Prefix notation for the given equation?
a|b&c
a) a|&bc
b) &|abc
c) |a&bc
d) ab&|c
View Answer
Answer: c
Explanation: The order of preference of operators is as follows (descending): & |.
The equation a|b&c will be parenthesized as (a|(b&c)) for evaluation.
Therefore the equation for prefix notation evaluates to |a&bc.
advertisement
a-b-c
a) -ab-c
b) ab – c –
c) – -abc
d) -ab-c
View Answer
Answer: b
Explanation: The corresponding postfix expression for the given infix expression is found to
be ab-c- and not abc- -.
a/b^c-d
a) abc^/d-
b) ab/cd^-
c) ab/^cd-
d) abcd^/-
View Answer
Answer: a
Explanation: Using the infix to postfix conversion algorithm, the corresponding postfix
expression for the infix expression is found to be abc^/d-.
10. Which of the following statement is incorrect with respect to infix to postfix conversion
algorithm?
a) operand is always placed in the output
b) operator is placed in the stack when the stack operator has lower precedence
c) parenthesis are included in the output
d) higher and equal priority operators follow the same condition
View Answer
Answer: c
Explanation: Parentheses are not included in the output. They are placed in the operator
stack and then discarded.
11. In infix to postfix conversion algorithm, the operators are associated from?
a) right to left
b) left to right
c) centre to left
d) centre to right
View Answer
Answer: b
Explanation: In infix, prefix and postfix expressions, the operators are associated from left to
right and not right to left.
12. What is the corresponding postfix expression for the given infix expression?
a*(b+c)/d
a) ab*+cd/
b) ab+*cd/
c) abc*+/d
d) abc+*d/
View Answer
Answer: d
Explanation: Using the infix to postfix conversion algorithm, the corresponding postfix
expression is obtained as abc+*d/.
13. What is the corresponding postfix expression for the given infix expression?
a+(b*c(d/e^f)*g)*h)
a) ab*cdef/^*g-h+
b) abcdef^/*g*h*+
c) abcd*^ed/g*-h*+
d) abc*de^fg/*-*h+
View Answer
Answer: b
Explanation: Using the infix to postfix expression conversion algorithm using stack, the
corresponding postfix expression is found to be abcdef^/*g*h*+.
14. What is the correct postfix expression for the following expression?
a+b*(c^d-e)^(f+g*h)-i
a) abc^de-fg+*^*+i-
b) abcde^-fg*+*^h*+i-
c) abcd^e-fgh*+^*+i-
d) ab^-dc*+ef^gh*+i-
View Answer
Answer: c
Explanation: The postfix expression for the given infix expression is found to be abcd^e-
fgh*+^*+i- when we use infix to postfix conversion algorithm.
15. From the given Expression tree, identify the correct postfix expression from the list of
options.
a) ab*cd*+
b) ab*cd-+
c) abcd-*+
d) ab*+cd-
View Answer
Answer: b
Explanation: From the given expression tree, the infix expression is found to be (a*b)+(c-d).
Converting it to postfix, we get, ab*cd-+.
- + 5 / 10 5 5
a) 2
b) 5
c) 10
d) 7
View Answer
Answer: a
Explanation: The infix notation of the given prefix notation is 5+10/5-5 which gives us 2 as
our answer.
advertisement
+ 9 * 3 / 8 4
a) 14
b) 15
c) 18
d) 12
View Answer
Answer: b
Explanation: The infix notation for the given prefix notation is (9+(3*(8/4))) which solves to
15. So 15 is correct answer.
- + 1 2 * 3 / 6 2
a) 6
b) -6
c) 3
d) -3
View Answer
Answer: b
Explanation: The infix notation for the given prefix notation is (1+2)-3*(6/2). The result of the
given equation is -6.
- * 1 5 / * / 6 3 6 2
a) 1
b) 0
c) -1
d) -2
View Answer
Answer: c
Explanation: The infix notation for the given prefix notation is (1*5)-(6/3)*6/2. The result of
the equation is -1.
* / + 1 2 / 4 2 + 3 5
a) 12
b) 7.5
c) 9
d) 13.5
View Answer
Answer: a
Explanation: The infix notation of the given prefix notation is ((1+2)/(4/2))*(3+5) which
solves to (3/2)*8 which by solving gives us 12.
7. Given a prefix and a postfix notation what are the difference between them?
a) The postfix equation is solved starting from the left whereas the prefix notation is solved
from the right
b) The postfix equation is solved starting from the right whereas the prefix notation is solved
from the left
c) Both equations are solved starting from the same side(right)
d) Both equations are solved starting from the same side(left)
View Answer
Answer: a
Explanation: The postfix notation is solved starting from left but whereas the prefix notation
is reversed after creating them, therefore it’s solved starting from right.
8. When converting the prefix notation into an infix notation, the first step to be followed is
________
a) Reverse the equation
b) Push the equation to the stack
c) Push the equation onto the queue
d) Push the equation to the stack or queue
View Answer
Answer: a
Explanation: The steps that are followed are: the equation is reversed, pushed onto a stack,
popped one by one and solved. Therefore the first step is reversing the equation.
10. Given two processes (conversion of postfix equation to infix notation and conversion of
prefix notation to infix notation), which of the following is easier to implement?
a) Both are easy to implement
b) Conversion of postfix equation to infix equation is harder than converting a prefix notation
to infix notation
c) Conversion of postfix equation to infix equation is easier than converting a prefix notation
to infix notation
d) Insufficient data
View Answer
Answer: c
Explanation: As the conversion of prefix notation to infix notation involves reversing the
equation, the latter is harder to implement than postfix to infix process.
1. Which of the following data structure is used to convert postfix expression to infix
expression?
a) Stack
b) Queue
c) Linked List
d) Heap
View Answer
Answer: a
Explanation: To convert the postfix expression into infix expression we need stack. We
need stack to maintain the intermediate infix expressions. We use stack to hold operands.
2. The postfix expression abc+de/*- is equivalent to which of the following infix expression?
a) abc+-de*/
b) (a+b)-d/e*c
c) a-(b+c)*(d/e)
d) abc+*-(d/e)
View Answer
Answer: c
infix ⇒ a(b+c)(d/e)*-
Explanation: Given postfix expression : abc+de/*-
⇒ a(b+c)*(d/e)-
⇒ a-(b+c)*(d/e)
Hence, correct choice is a-(b+c)*(d/e).
3. The equivalent infix expression and value for the postfix form 1 2 + 3 * 4 5 * – will be
___________
a) 1 + 2 * 3 – 4 * 5 and -13
b) (2 + 1) * (3 – 4) * 5 and 13
c) 1 + 2 * (3 – 4) * 5 and -11
d) (1 + 2) * 3 – (4 * 5) and -11
View Answer
Answer: d
⇒ (1 + 2) 3 * 4 5 * –
Explanation: Given postfix expression : 1 2 + 3 * 4 5 * –
⇒ ((1 + 2) * 3) 4 5 * –
⇒ ((1 + 2) * 3) (4 * 5) –
⇒ ((1 + 2) * 3) – (4 * 5)
So, the equivalent infix expression is (1 + 2) * 3 – (4 * 5) and it’s value is -11.
advertisement
infix ⇒ (2 + 3)4 (5 – 6) – *
Explanation: Given postfix expression : 2 3 + 4 5 6 – – *
⇒ (2 + 3)*4 – (5 – 6)
Hence, value = (2 + 3) * (4 – (5 – 6)) = 5 *(4 – (-1)) = 5*5 = 25.
infix ⇒ 4 (5 a 6) b (7 a 8) c
Explanation: Given postfix expression: 4 5 6 a b 7 8 a c
⇒ (4 b (5 a 6)) (7 a 8) c
⇒ (4 b (5 a 6)) c (7 a 8)
So, the required infix expression is 4 b 5 a 6 c 7 a 8.
7. To convert the postfix expression into the infix expression we use stack and scan the
postfix expression from left to right.
a) True
b) False
View Answer
Answer: a
Explanation: Stack is used to postfix expression to infix expression. And to convert we
follow the following steps: (i) Scan the expression from left to right. (ii) If operand is found,
push it on stack.(iii) If operator is found, the two operands are popped and the combined
infix expression is formed and pushed onto the stack.
2. Select the appropriate code for the recursive Tower of Hanoi problem.(n is the number of
disks)
a)
advertisement
Answer: a
Explanation: First transfer all the diska to the auxiliary and then to the end peg, this is
achieved by making auxiliary peg as the end peg in the first recursive call, in the second
recursive call, the auxiliary becomes the start peg from where the disks are transferred to
the end peg.
Answer: a
Explanation: Push all the characters in the input string to a stack, now pop them and
append to a new string which is checked for equality with the original string.
6. What is the number of moves required to solve Tower of Hanoi problem for k disks?
a) 2k – 1
b) 2k + 1
c) 2k + 1
d) 2k – 1
View Answer
Answer: d
Explanation: Tracing of the moves in the above ToH problem will prove this result, instead
you can simply add a count for each recursive call to check the number of moves.
Answer: b
Explanation: Although, it is possible to reverse the string without using stack, it is done by
looping through the string from the end character by character.
In Java, it is also possible to use the StringBuilder and StringBuffer classes which have a
built-in method ‘reverse’.
Note its similarity to PalindromeTes
1. Reversing a word using stack can be used to find if the given word is a palindrome or not.
a) True
b) False
View Answer
Answer: a
Explanation: This application of stack can also be used to find if the given word is a
palindrome because, if the reversed is same as that of the original word, the given word is a
palindrome.
3. Operations required for reversing a word or a string using stack are push() and pop().
a) True
b) False
View Answer
Answer: a
Explanation: Push operation inserts a character into the stack and pop operation pops the
top of the stack.
advertisement
5. What will be the word obtained if the word “abbcabb” is reversed using a stack?
a) bbabbca
b) abbcabb
c) bbacbba
d) bbacabb
View Answer
Answer: c
Explanation: The string “abbcabb” is pushed on to the stack. If the characters are popped
one by one, the word obtained will be bbacbba.
a) pat
b) tap
c) atp
d) apt
View Answer
Answer: b
Explanation: The word ‘pat’ is pushed on to the stack. When the characters of the stack are
popped one by one, the word ‘tap’ is obtained.
Push(a,s);
Push(b,s);
Pop(b);
Push(c,s);
a) abc
b) b
c) ac
d) acb
View Answer
Answer: b
Explanation: The element ‘b’ is popped out of the stack. Hence the output of the following
sequence of operations will be ‘b’.
9. What are the set of functions that are to be executed to get the following output?
cat
a) push(c, s); push(a, s); push(t, s);
pop(s); pop(s); pop(s);
b) push(c,s); pop(s); push(a,s); pop(s);push(t,s);pop(s);
c) pop(c ); pop(a); pop(t);
d) push(c,s); push(a,s); pop(t);
View Answer
Answer: b
Explanation: During push operation, the characters ‘c’, ’a’, ’t’ are inserted into the stack and
popped immediately after push.
10. How will your stack look like if the word ‘java’ is pushed?
a)
b)
c)
d)
View Answer
Answer: a
Explanation: When a character is pushed, it stays on the top of the stack. While popping,
the word occurs in reverse order since stack follows LIFO principle.
11. Find the error (if any) in the following code snippet for pop operation.
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) sanfoundry
b) san foundry
c) yrdnuof nas
d) foundry nas
View Answer
Answer: c
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’
advertisement
4. Which is the most appropriate data structure for applying balancing of symbols
algorithm?
a) stack
b) queue
c) tree
d) graph
View Answer
Answer: a
Explanation: Stack is the most appropriate data structure for balancing symbols algorithm
because stack follows LIFO principle (Last In First Out).
10. An error is reported when the stack is not empty at the end.
a) True
b) False
View Answer
Answer: a
Explanation: When the stack contains elements at the end, it means that the given string of
parentheses is not balanced.
11. Is the given statement ((A+B) + [C-D]] valid with respect to balancing of symbols?
a) True
b) False
View Answer
Answer: b
Explanation: The given statement is invalid with respect to balancing of symbols because
the last parentheses do not correspond to the opening braces.
12. How many passes does the balancing symbols algorithm makes through the input?
a) one
b) two
c) three
d) four
View Answer
Answer: a
Explanation: The balancing symbols algorithm makes only one pass through the input since
it is linear.
13. Which of the following statement is invalid with respect to balancing symbols?
a) [(A+B) + (C-D)]
b) [{A+B}-{C-[D+E]}]
c) ((A+B) + (C+D)
d) {(A+B) + [C+D]}
View Answer
Answer: c
Explanation: ((A+B) + (C+D) is invalid because the last close parentheses is not found in
the statement.
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.
advertisement
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!
advertisement
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)
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.
6. How will you find the minimum element in a binary search tree?
a)
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)
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)
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. 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.
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.
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).
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.
2. The binary tree sort implemented using a self – balancing binary search tree takes
______ time is worst case.
a) O(n log n)
b) O(n)
c) O(n2)
d) O(log n)
View Answer
Answer: a
Explanation: The worst case running time of the binary tree sort is O(n2). But, the worst case
running time can be improved to the O(n log n) using a self – balancing binary search tree.
3. An AVL tree is a self – balancing binary search tree, in which the heights of the two child
sub trees of any node differ by _________
a) At least one
b) At most one
c) Two
d) At most two
View Answer
Answer: b
Explanation: In an AVL tree, the difference between heights of the two child sub trees of any
node is at most one. If the height differs by more than one, AVL tree performs rotations to
balance the tree.
advertisement
8. In which of the following self – balancing binary search tree the recently accessed
element can be accessed quickly?
a) AVL tree
b) AA tree
c) Splay tree
d) Red – Black tree
View Answer
Answer: c
Explanation: In a Splay tree, the recently accessed element can be accessed quickly. In
Splay tree, the frequently accessed nodes are moved towards the root so they are quick to
access again.
9. The minimum height of self balancing binary search tree with n nodes is _____
a) log2(n)
b) n
c) 2n + 1
d) 2n – 1
View Answer
Answer: a
Explanation: Self – balancing binary trees adjust the height by performing transformations
on the tree at key insertion times, in order to keep the height proportional to log2(n).
10. Binary tree sort implemented using a self balancing binary search tree takes O(n log n)
time in the worst case but still it is slower than merge sort.
a) True
b) False
View Answer
Answer: a
Explanation: The worst case performance of binary tree sort is O(n log n) when it is
implemented using a self balancing binary search tree. Self balancing binary search trees
perform transformations to balance the tree, which caused balancing overhead. Due to this
overhead, binary tree sort is slower than merger sort
advertisement
7. What is the prime condition of AA-tree which makes it simpler than a red-black tree?
a) Only right children can be red
b) Only left children can be red
c) Right children should strictly be black
d) There should be no left children
View Answer
Answer: a
Explanation: The prime condition of AA-Tree is that only the right children can be red to
eliminate possible restructuring cases.
12. What should be the condition for the level of a left node?
a) It should be less than or equal to that of its parent
b) It should be greater than that of its parent
c) It should be strictly less than that of its parent
d) The level should be equal to one
View Answer
Answer: c
Explanation: The level of a left node should be strictly less than that of its parent. The level
of a right node is less than or equal to that of its parent.
13. Of the following rules that are followed by an AA-tree, which of the following is
incorrect?
1- Only right children can be red
2- Procedures are coded recursively
3- Instead of storing colors, the level of a node is stored
4- There should not be any left children
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: d
Explanation: In an AA-Tree, both left and right children can be present. The only condition is
that only right children can be red.
a) left rotation
b) right rotation
c) insertion
d) deletion
View Answer
Answer: b
Explanation: B is initially the right child of X. It is then rotated right side and now, B is the left
child of P.
15. Comparing the speed of execution of Red-Black trees and AA-trees, which one has the
faster search time?
a) AA-tree
b) Red-Black tree
c) Both have an equal search time
d) It depends
View Answer
Answer: a
Explanation: Since an AA-tree tends to be flatter, AA-tree has a faster search time than a
Red-Black tree.
i.
ii.
a) only i
b) only i and ii
c) only ii
d) i is not a binary search tree
View Answer
Answer: b
Explanation: The property of AVL tree is it is height balanced tree with difference of atmost
1 between left and right subtrees. All AVL trees are binary search tree.
advertisement
5. To restore the AVL property after inserting a element, we start at the insertion point and
move towards root of that tree. is this statement true?
a) true
b) false
View Answer
Answer: a
Explanation: It is interesting to note that after insertion, only the path from that point to node
or only that subtrees are imbalanced interms of height.
6. Given an empty AVL tree, how would you construct AVL tree when a set of numbers are
given without performing any rotations?
a) just build the tree with the given input
b) find the median of the set of elements given, make it as root and construct the tree
c) use trial and error
d) use dynamic programming to build the tree
View Answer
Answer: b
Explanation: Sort the given input, find the median element among them, make it as root and
construct left and right subtrees with elements lesser and greater than the median element
recursively. this ensures the subtrees differ only by height 1.
7. What maximum difference in heights between the leafs of a AVL tree is possible?
a) log(n) where n is the number of nodes
b) n where n is the number of nodes
c) 0 or 1
d) atmost 1
View Answer
Answer: a
Explanation: At every level we can form a tree with difference in height between subtrees to
be atmost 1 and so there can be log(n) such levels since height of AVL tree is log(n).
if(left_tree_height== -1)
return left_tree_height
right_tree_height= avl(right_of_root)
if(right_tree_height==-1)
return right_tree_height
Does the above code can check if a binary search tree is an AVL tree?
a) yes
b) no
View Answer
Answer: a
Explanation: The condition to check the height difference between left and right subtrees is
missing. if (absolute(left_tree_height – right_tree_height)>1) must be added.
9. Consider the below left-left rotation pseudo code where the node contains value pointers
to left, right child nodes and a height value and Height() function returns height value stored
at a particular node.
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).
advertisement
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).
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.
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