Data Structure and Algorithm MCQs
Data Structure and Algorithm 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?
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.
37. In simple chaining, what data structure is appropriate?
a) Doubly linked list
b) Circular linked list
c) Singly linked list
d) Binary trees
View Answer
Answer: a
Explanation: Deletion becomes easier with doubly linked list, hence it is appropriate.
Chapter # 01