0% found this document useful (0 votes)
54 views

MCQs On Data Structures and Algorithms

MCQs on Data Structures and Algorithms

Uploaded by

Prokash Barman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

MCQs On Data Structures and Algorithms

MCQs on Data Structures and Algorithms

Uploaded by

Prokash Barman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Structures and Algorithms

(MCQs and Answer with explanation)

Question 1
Which of the following is a linear data structure?

A) Tree
B) Graph
C) Array
D) Hash Table
Answer: C) Array
Explanation: An array is a linear data structure where elements are stored in
contiguous memory locations. Trees and graphs are non-linear data structures, and
hash tables are a form of associative arrays, which can be implemented using
various data structures.

Question 2
What is the time complexity of the binary search algorithm?

A) O(n)
B) O(log n)
C) O(n^2)
D) O(1)
Answer: B) O(log n)
Explanation: Binary search works by repeatedly dividing the search interval in half. If
the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half. Otherwise, narrow it to the upper half. This process
reduces the time complexity to O(log n).

Question 3
Which algorithm is used to sort a list of elements using the divide and conquer
approach?

A) Bubble Sort
B) Selection Sort
C) Merge Sort
D) Insertion Sort
Answer: C) Merge Sort
Explanation: Merge sort is a divide and conquer algorithm that divides the input array
into two halves, calls itself for the two halves, and then merges the two sorted halves.
The time complexity of merge sort is O(n log n).

Question 4
What is the maximum number of edges in an undirected graph with n vertices?
A) n
B) n^2
C) n(n-1)/2
D) n(n+1)/2
Answer: C) n(n-1)/2
Explanation: In an undirected graph, each pair of vertices can have at most one edge
between them. Therefore, the maximum number of edges is given by the formula
n(n-1)/2, where n is the number of vertices.

Question 5
Which data structure is used to implement a queue?

A) Stack
B) Linked List
C) Array
D) Both B and C
Answer: D) Both B and C
Explanation: A queue can be implemented using both linked lists and arrays. Linked
lists are more efficient for dynamic sizes, while arrays are simpler for fixed sizes.

Question 6
What is the best-case time complexity of quick sort?

A) O(n log n)
B) O(n)
C) O(n^2)
D) O(1)
Answer: A) O(n log n)
Explanation: The best-case time complexity of quick sort occurs when the
partitioning is balanced, resulting in a time complexity of O(n log n).

Question 7
Which of the following is a stable sorting algorithm?

A) Bubble Sort
B) Selection Sort
C) Quick Sort
D) Heap Sort
Answer: A) Bubble Sort
Explanation: Bubble sort is a stable sorting algorithm because it maintains the
relative order of equal elements. Selection sort, quick sort, and heap sort are not
stable.

Question 8
What is the purpose of a hash function in a hash table?
A) To store data
B) To sort data
C) To index data
D) To search data
Answer: C) To index data
Explanation: A hash function in a hash table is used to convert a key into an index
that can be used to store and retrieve data efficiently.

Question 9
Which of the following is a true statement about binary trees?

A) Every node has exactly two children.


B) Every level of the tree is completely filled.
C) All nodes are as far left as possible.
D) The maximum number of nodes at level 'i' is 2^i.
Answer: D) The maximum number of nodes at level 'i' is 2^i.
Explanation: In a binary tree, the maximum number of nodes at any level 'i' is 2^i. This
is because each node can have at most two children.

Question 10
What is the space complexity of the recursive implementation of the binary search
algorithm?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: B) O(log n)
Explanation: The space complexity of the recursive implementation of the binary
search algorithm is O(log n) because the recursion stack can have at most log n
levels.

Question 11
Which of the following is a non-linear data structure?

A) Array
B) Linked List
C) Tree
D) Stack
Answer: C) Tree
Explanation: A tree is a non-linear data structure where elements are organized in a
hierarchical manner. Arrays, linked lists, and stacks are linear data structures.

Question 12
What is the time complexity of the best-case scenario for quick sort?
A) O(n log n)
B) O(n)
C) O(n^2)
D) O(1)
Answer: A) O(n log n)
Explanation: The best-case time complexity of quick sort is O(n log n), which occurs
when the partitioning is balanced.

Question 13
Which data structure is used to implement a stack?

A) Array
B) Linked List
C) Both A and B
D) None of the above
Answer: C) Both A and B
Explanation: A stack can be implemented using both arrays and linked lists. Arrays
are typically used for static stacks, while linked lists are used for dynamic stacks.

Question 14
What is the maximum number of edges in a directed graph with n vertices?

A) n
B) n^2
C) n(n-1)/2
D) n(n+1)/2
Answer: B) n^2
Explanation: In a directed graph, each pair of vertices can have two edges (one in
each direction). Therefore, the maximum number of edges is n(n-1), but since a vertex
cannot have an edge to itself, the maximum is n^2.

Question 15
Which of the following sorting algorithms is not stable?

A) Bubble Sort
B) Merge Sort
C) Quick Sort
D) Insertion Sort
Answer: C) Quick Sort
Explanation: Quick sort is not stable because the relative order of equal elements is
not preserved in its standard implementation. Bubble sort, merge sort, and insertion
sort are stable sorting algorithms.

Question 16
What is the purpose of a hash table?
A) To store data efficiently
B) To sort data
C) To index data
D) To search data
Answer: A) To store data efficiently
Explanation: The primary purpose of a hash table is to store and retrieve data
efficiently by using a hash function to compute an index into an array of buckets from
which the desired value can be found.

Question 17
Which of the following is a true statement about binary search trees (BST)?

A) Every node has exactly two children.


B) The left subtree of a node contains only nodes with keys greater than the node's
key.
C) The right subtree of a node contains only nodes with keys less than the node's key.
D) The left and right subtrees of a node must also be binary search trees.
Answer: D) The left and right subtrees of a node must also be binary search trees.
Explanation: In a binary search tree, the left subtree of a node contains only nodes
with keys less than the node's key, and the right subtree contains only nodes with keys
greater than the node's key. Additionally, both the left and right subtrees must also be
binary search trees.

Question 18
What is the space complexity of the iterative implementation of the binary search
algorithm?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: A) O(1)
Explanation: The iterative implementation of the binary search algorithm does not
use recursion, so it has a space complexity of O(1) because it does not require a
recursion stack.

Question 19
Which of the following is a correct implementation of a queue using two stacks?

A) Enqueue operation: Push onto the first stack. Dequeue operation: Pop from the
second stack if not empty, else transfer all elements from the first stack to the second
stack and then pop.
B) Enqueue operation: Push onto the second stack. Dequeue operation: Pop from the
first stack if not empty, else transfer all elements from the second stack to the first
stack and then pop.
C) Enqueue operation: Push onto both stacks. Dequeue operation: Pop from the first
stack.
D) Enqueue operation: Push onto the first stack. Dequeue operation: Pop from the first
stack.
Answer: A) Enqueue operation: Push onto the first stack. Dequeue operation: Pop
from the second stack if not empty, else transfer all elements from the first stack to
the second stack and then pop.
Explanation: This implementation ensures that the elements are dequeued in the
correct order by transferring them from the first stack to the second stack when
necessary.

Question 20
What is the time complexity of the heap sort algorithm?

A) O(n log n)
B) O(n)
C) O(n^2)
D) O(1)
Answer: A) O(n log n)
Explanation: The time complexity of the heap sort algorithm is O(n log n) because it
involves building a heap and then repeatedly extracting the maximum element, each
operation taking O(log n) time.

Question 21
Which of the following is a correct way to implement a dynamic array?

A) Using a fixed-size array and resizing it manually when needed.


B) Using a linked list to store the elements.
C) Using a hash table to store the elements.
D) Using a stack to store the elements.
Answer: A) Using a fixed-size array and resizing it manually when needed.
Explanation: A dynamic array is typically implemented using a fixed-size array that is
resized manually when more space is needed. This allows the array to grow or shrink
dynamically.

Question 22
What is the time complexity of the insertion operation in a binary search tree (BST)
in the worst case?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: In the worst case, when the binary search tree is skewed (e.g., all
elements are inserted in sorted order), the insertion operation can take O(n) time
because it may need to traverse the entire height of the tree.

Question 23
Which of the following is a correct implementation of a stack using a queue?

A) Push operation: Enqueue the element. Pop operation: Dequeue all elements except
the last one and then dequeue the last one.
B) Push operation: Enqueue the element. Pop operation: Dequeue all elements and
then enqueue them again.
C) Push operation: Enqueue the element. Pop operation: Dequeue the first element.
D) Push operation: Enqueue the element. Pop operation: Dequeue all elements except
the first one and then dequeue the first one.
Answer: A) Push operation: Enqueue the element. Pop operation: Dequeue all
elements except the last one and then dequeue the last one.
Explanation: To implement a stack using a queue, you need to ensure that the last
element enqueued is the first one to be dequeued. This can be achieved by
dequeuing all elements except the last one and then dequeuing the last one.

Question 24
What is the purpose of a bloom filter?

A) To store data efficiently


B) To sort data
C) To index data
D) To test whether an element is a member of a set
Answer: D) To test whether an element is a member of a set
Explanation: A bloom filter is a space-efficient probabilistic data structure used to
test whether an element is a member of a set. It allows for a small false positive
probability but no false negatives.

Question 25
What is the time complexity of the delete operation in a hash table with a good hash
function?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: A) O(1)
Explanation: With a good hash function, the delete operation in a hash table can be
performed in O(1) time because it involves directly accessing the element's location
using the hash function.

Question 26
Which of the following is a true statement about AVL trees?
A) They are a type of binary search tree.
B) They are a type of linked list.
C) They are a type of queue.
D) They are a type of stack.
Answer: A) They are a type of binary search tree.
Explanation: AVL trees are a type of binary search tree that is height-balanced. They
are designed to maintain balance after insertion and deletion operations.

Question 27
What is the time complexity of the merge operation in merge sort?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: The merge operation in merge sort takes O(n) time because it involves
traversing the two subarrays and merging them into a single sorted array.

Question 28
Which of the following is a correct way to implement a priority queue?

A) Using an array
B) Using a linked list
C) Using a heap
D) Using a stack
Answer: C) Using a heap
Explanation: A priority queue is typically implemented using a heap because it allows
for efficient insertion and deletion of the maximum (or minimum) element.

Question 29
What is the purpose of a trie data structure?

A) To store data efficiently


B) To sort data
C) To index data
D) To store and retrieve strings efficiently
Answer: D) To store and retrieve strings efficiently
Explanation: A trie, also known as a prefix tree, is a data structure used to store and
retrieve strings efficiently. It is particularly useful for string matching and prefix
searches.

Question 30
What is the time complexity of the Dijkstra's algorithm for finding the shortest path
in a graph with n vertices and m edges?
A) O(n log n)
B) O(n^2)
C) O(m log n)
D) O(n^2 + m)
Answer: C) O(m log n)
Explanation: The time complexity of Dijkstra's algorithm is O(m log n) when
implemented using a binary heap. This is because each edge is processed once, and
the heap operations (extract-min and decrease-key) take O(log n) time.

Question 31
Which of the following is a correct way to implement a queue using two stacks?

A) Enqueue operation: Push onto the first stack. Dequeue operation: Pop from the
second stack if not empty, else transfer all elements from the first stack to the second
stack and then pop.
B) Enqueue operation: Push onto the second stack. Dequeue operation: Pop from the
first stack if not empty, else transfer all elements from the second stack to the first
stack and then pop.
C) Enqueue operation: Push onto both stacks. Dequeue operation: Pop from the first
stack.
D) Enqueue operation: Push onto the first stack. Dequeue operation: Pop from the first
stack.
Answer: A) Enqueue operation: Push onto the first stack. Dequeue operation: Pop
from the second stack if not empty, else transfer all elements from the first stack to
the second stack and then pop.
Explanation: This implementation ensures that the elements are dequeued in the
correct order by transferring them from the first stack to the second stack when
necessary.

Question 32
What is the time complexity of the insertion operation in a trie?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: B) O(log n)
Explanation: The insertion operation in a trie has a time complexity of O(log n)
because it involves traversing the trie depth-first, which is logarithmic with respect to
the number of nodes.

Question 33
Which of the following is a true statement about red-black trees?
A) They are a type of binary search tree.
B) They are a type of linked list.
C) They are a type of queue.
D) They are a type of stack.
Answer: A) They are a type of binary search tree.
Explanation: Red-black trees are a type of self-balancing binary search tree that
maintains balance after insertion and deletion operations.

Question 34
What is the purpose of a segment tree?

A) To store data efficiently


B) To sort data
C) To index data
D) To perform range queries and updates efficiently
Answer: D) To perform range queries and updates efficiently
Explanation: A segment tree is a data structure used to perform range queries and
updates over an array efficiently. It allows for operations like range sum, range
minimum/maximum, and range update.

Question 35
What is the time complexity of the build operation in a segment tree?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: The build operation in a segment tree has a time complexity of O(n)
because it involves creating the segment tree from the input array, which requires
processing each element once.

Question 36
Which of the following is a correct way to implement a set?

A) Using an array
B) Using a linked list
C) Using a hash table
D) Using a stack
Answer: C) Using a hash table
Explanation: A set is typically implemented using a hash table because it allows for
efficient insertion, deletion, and membership testing.

Question 37
What is the purpose of a binary indexed tree (Fenwick tree)?
A) To store data efficiently
B) To sort data
C) To index data
D) To perform cumulative frequency and range sum queries efficiently
Answer: D) To perform cumulative frequency and range sum queries efficiently
Explanation: A binary indexed tree (Fenwick tree) is a data structure used to perform
cumulative frequency and range sum queries efficiently. It allows for operations like
finding the sum of elements in a given range and updating an element.

Question 38
What is the time complexity of the update operation in a binary indexed tree?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: B) O(log n)
Explanation: The update operation in a binary indexed tree has a time complexity of
O(log n) because it involves updating the tree by propagating the change up the
tree.

Question 39
Which of the following is a true statement about B-trees?

A) They are a type of binary search tree.


B) They are a type of linked list.
C) They are a type of queue.
D) They are a type of self-balancing tree used in databases.
Answer: D) They are a type of self-balancing tree used in databases.
Explanation: B-trees are a type of self-balancing tree data structure that is
commonly used in databases and filesystems to store and retrieve data efficiently.

Question 40
What is the purpose of a skip list?

A) To store data efficiently


B) To sort data
C) To index data
D) To provide a probabilistic alternative to balanced trees
Answer: D) To provide a probabilistic alternative to balanced trees
Explanation: A skip list is a data structure that provides a probabilistic alternative to
balanced trees. It allows for efficient search, insertion, and deletion operations.

Question 41
Which of the following is a correct way to implement a deque using a doubly linked
list?

A) Insert at the head and delete at the tail.


B) Insert at the tail and delete at the head.
C) Insert and delete at both the head and tail.
D) Insert at the head and delete at the head.
Answer: C) Insert and delete at both the head and tail.
Explanation: A deque (double-ended queue) allows for insertion and deletion at both
ends. Using a doubly linked list, you can efficiently insert and delete elements at both
the head and tail.

Question 42
What is the time complexity of the Bellman-Ford algorithm for finding the shortest
path in a graph with n vertices and m edges?

A) O(n log n)
B) O(n^2)
C) O(m log n)
D) O(mn)
Answer: D) O(mn)
Explanation: The Bellman-Ford algorithm has a time complexity of O(mn) because it
relaxes all edges up to n−1n−1 times, where each relaxation operation takes O(m)
time.

Question 43
Which of the following is a true statement about disjoint-set data structures?

A) They are used to maintain a collection of disjoint sets.


B) They are used to sort data.
C) They are used to index data.
D) They are used to store data efficiently.
Answer: A) They are used to maintain a collection of disjoint sets.
Explanation: Disjoint-set data structures, also known as union-find data structures,
are used to maintain a collection of disjoint sets and to perform operations like union
and find.

Question 44
What is the purpose of a topological sort in a directed acyclic graph (DAG)?

A) To sort the vertices in ascending order.


B) To sort the vertices in descending order.
C) To arrange the vertices in a linear order such that for every directed edge uv,
vertex u comes before vertex v.
D) To arrange the vertices in a circular order.
Answer: C) To arrange the vertices in a linear order such that for every directed
edge uv, vertex u comes before vertex v.
Explanation: A topological sort of a directed acyclic graph (DAG) is a linear ordering
of its vertices such that for every directed edge uv, vertex u comes before vertex v in
the ordering.

Question 45
What is the time complexity of the Kruskal's algorithm for finding the minimum
spanning tree of a graph with n vertices and m edges?

A) O(n log n)
B) O(n^2)
C) O(m log n)
D) O(mn)
Answer: C) O(m log n)
Explanation: Kruskal's algorithm sorts the edges by weight and then processes them
in ascending order. The sorting step takes O(m log m) time, but since m can be at
most n(n-1)/2, the overall time complexity is O(m log n).

Question 46
Which of the following is a correct way to implement a priority queue using a binary
heap?

A) Insert at the head and delete at the tail.


B) Insert at the tail and delete at the head.
C) Insert at the tail and delete at the tail.
D) Insert at the head and delete at the head.
Answer: B) Insert at the tail and delete at the head.
Explanation: In a binary heap, elements are inserted at the tail (the next available
position) and deleted from the head (the root), which contains the maximum or
minimum element, depending on whether it's a max heap or min heap.

Question 47
What is the purpose of a suffix array?

A) To store data efficiently


B) To sort data
C) To index data
D) To facilitate efficient pattern matching and searching in strings
Answer: D) To facilitate efficient pattern matching and searching in strings
Explanation: A suffix array is a data structure used for pattern matching and
searching in strings. It stores the suffixes of a string in lexicographical order, allowing
for efficient searches.

Question 48
What is the time complexity of the build operation in a binary heap?
A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: The build operation in a binary heap has a time complexity of O(n)
because it involves creating the heap from the input array, which can be done in
linear time using a bottom-up approach.

Question 49
Which of the following is a true statement about depth-first search (DFS)?

A) It is a breadth-first traversal algorithm.


B) It is a depth-first traversal algorithm.
C) It is a sorting algorithm.
D) It is a searching algorithm.
Answer: B) It is a depth-first traversal algorithm.
Explanation: Depth-first search (DFS) is an algorithm for traversing or searching tree
or graph data structures. It starts at the root (selecting some arbitrary node as the
root in the case of a graph) and explores as far as possible along each branch before
backtracking.

Question 50
What is the purpose of a breadth-first search (BFS) in a graph?

A) To find the shortest path between two vertices.


B) To traverse the graph in a depth-first manner.
C) To sort the vertices of the graph.
D) To index the vertices of the graph.
Answer: A) To find the shortest path between two vertices.
Explanation: Breadth-first search (BFS) is used to find the shortest path between two
vertices in terms of the number of edges. It traverses the graph level by level.

Question 51
Which of the following is a correct way to implement a min heap?

A) The value of each node is greater than or equal to the value of its parent.
B) The value of each node is less than or equal to the value of its parent.
C) The value of each node is equal to the value of its parent.
D) The value of each node is unrelated to the value of its parent.
Answer: B) The value of each node is less than or equal to the value of its parent.
Explanation: In a min heap, the value of each node is less than or equal to the value
of its parent. This property ensures that the minimum element is always at the root of
the heap.
Question 52
What is the time complexity of the Floyd-Warshall algorithm for finding the
shortest path in a graph with n vertices?

A) O(n log n)
B) O(n^2)
C) O(n^3)
D) O(2^n)
Answer: C) O(n^3)
Explanation: The Floyd-Warshall algorithm has a time complexity of O(n^3) because
it involves three nested loops, each iterating over all vertices.

Question 53
Which of the following is a true statement about dynamic programming?

A) It is a divide and conquer approach.


B) It is a greedy approach.
C) It is an approach where the problem is solved by breaking it down into simpler
sub-problems.
D) It is an approach where the problem is solved by considering the global optimum
directly.
Answer: C) It is an approach where the problem is solved by breaking it down into
simpler sub-problems.
Explanation: Dynamic programming is an approach where the problem is solved by
breaking it down into simpler sub-problems and storing the results of sub-problems
to avoid recomputation.

Question 54
What is the purpose of a memoization technique in dynamic programming?

A) To store data efficiently


B) To sort data
C) To index data
D) To reuse the results of sub-problems
Answer: D) To reuse the results of sub-problems
Explanation: Memoization is a technique used in dynamic programming to store the
results of sub-problems so that they can be reused later, avoiding redundant
computations.

Question 55
Which of the following is a correct way to implement a max heap?

A) The value of each node is greater than or equal to the value of its parent.
B) The value of each node is less than or equal to the value of its parent.
C) The value of each node is equal to the value of its parent.
D) The value of each node is unrelated to the value of its parent.
Answer: A) The value of each node is greater than or equal to the value of its parent.
Explanation: In a max heap, the value of each node is greater than or equal to the
value of its parent. This property ensures that the maximum element is always at the
root of the heap.

Question 56
What is the time complexity of the Prim's algorithm for finding the minimum
spanning tree of a graph with n vertices and m edges?

A) O(n log n)
B) O(n^2)
C) O(m log n)
D) O(mn)
Answer: C) O(m log n)
Explanation: Prim's algorithm uses a priority queue to select the minimum weight
edge. The time complexity is O(m log n) because each edge is processed once, and
the priority queue operations (insert and extract-min) take O(log n) time.

Question 57
Which of the following is a true statement about the Knuth-Morris-Pratt (KMP)
algorithm?

A) It is a sorting algorithm.
B) It is a string matching algorithm.
C) It is a graph traversal algorithm.
D) It is a dynamic programming algorithm.
Answer: B) It is a string matching algorithm.
Explanation: The Knuth-Morris-Pratt (KMP) algorithm is a string matching algorithm
that searches for occurrences of a pattern within a text. It uses a partial match table
to avoid unnecessary comparisons.

Question 58
What is the purpose of a hash function in a hash table?

A) To store data efficiently


B) To sort data
C) To index data
D) To search data
Answer: C) To index data
Explanation: A hash function in a hash table is used to convert a key into an index
that can be used to store and retrieve data efficiently.

Question 59
Which of the following is a correct way to handle collisions in a hash table?

A) Linear probing
B) Chaining
C) Rehashing
D) All of the above
Answer: D) All of the above
Explanation: Collisions in a hash table can be handled using various techniques such
as linear probing, chaining, and rehashing. Each method has its own advantages and
disadvantages.

Question 60
What is the purpose of a bloom filter?

A) To store data efficiently


B) To sort data
C) To index data
D) To test whether an element is a member of a set
Answer: D) To test whether an element is a member of a set
Explanation: A bloom filter is a space-efficient probabilistic data structure used to
test whether an element is a member of a set. It allows for a small false positive
probability but no false negatives.

Question 61
Which of the following is a correct way to implement a queue using a singly linked
list?

A) Enqueue operation: Insert at the head. Dequeue operation: Delete at the head.
B) Enqueue operation: Insert at the tail. Dequeue operation: Delete at the head.
C) Enqueue operation: Insert at the head. Dequeue operation: Delete at the tail.
D) Enqueue operation: Insert at the tail. Dequeue operation: Delete at the tail.
Answer: B) Enqueue operation: Insert at the tail. Dequeue operation: Delete at the
head.
Explanation: In a queue implemented using a singly linked list, the enqueue operation
involves inserting an element at the tail of the list, and the dequeue operation involves
deleting an element from the head of the list.

Question 62
What is the time complexity of the insertion operation in a hash table with a good
hash function?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: A) O(1)
Explanation: With a good hash function, the insertion operation in a hash table can
be performed in O(1) time because it involves directly accessing the element's
location using the hash function.
Question 63
Which of the following is a true statement about the Boyer-Moore algorithm?

A) It is a sorting algorithm.
B) It is a string matching algorithm.
C) It is a graph traversal algorithm.
D) It is a dynamic programming algorithm.
Answer: B) It is a string matching algorithm.
Explanation: The Boyer-Moore algorithm is a string matching algorithm that searches
for occurrences of a pattern within a text. It uses a bad character rule and a good
suffix rule to skip comparisons.

Question 64
What is the purpose of a trie data structure?

A) To store data efficiently


B) To sort data
C) To index data
D) To store and retrieve strings efficiently
Answer: D) To store and retrieve strings efficiently
Explanation: A trie, also known as a prefix tree, is a data structure used to store and
retrieve strings efficiently. It is particularly useful for string matching and prefix
searches.

Question 65
What is the time complexity of the build operation in a trie?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: The build operation in a trie has a time complexity of O(n) because it
involves inserting each element into the trie, which takes O(1) time per element.

Question 66
Which of the following is a correct way to implement a stack using a queue?

A) Push operation: Enqueue the element. Pop operation: Dequeue all elements except
the last one and then dequeue the last one.
B) Push operation: Enqueue the element. Pop operation: Dequeue all elements and
then enqueue them again.
C) Push operation: Enqueue the element. Pop operation: Dequeue the first element.
D) Push operation: Enqueue the element. Pop operation: Dequeue all elements except
the first one and then dequeue the first one.
Answer: A) Push operation: Enqueue the element. Pop operation: Dequeue all
elements except the last one and then dequeue the last one.
Explanation: To implement a stack using a queue, you need to ensure that the last
element enqueued is the first one to be dequeued. This can be achieved by
dequeuing all elements except the last one and then dequeuing the last one.

Question 67
What is the purpose of a segment tree?

A) To store data efficiently


B) To sort data
C) To index data
D) To perform range queries and updates efficiently
Answer: D) To perform range queries and updates efficiently
Explanation: A segment tree is a data structure used to perform range queries and
updates over an array efficiently. It allows for operations like range sum, range
minimum/maximum, and range update.

Question 68
What is the time complexity of the update operation in a segment tree?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: B) O(log n)
Explanation: The update operation in a segment tree has a time complexity of O(log
n) because it involves updating the tree by propagating the change up the tree.

Question 69
Which of the following is a true statement about red-black trees?

A) They are a type of binary search tree.


B) They are a type of linked list.
C) They are a type of queue.
D) They are a type of stack.
Answer: A) They are a type of binary search tree.
Explanation: Red-black trees are a type of self-balancing binary search tree that
maintains balance after insertion and deletion operations.

Question 70
What is the purpose of a binary indexed tree (Fenwick tree)?

A) To store data efficiently


B) To sort data
C) To index data
D) To perform cumulative frequency and range sum queries efficiently
Answer: D) To perform cumulative frequency and range sum queries efficiently
Explanation: A binary indexed tree (Fenwick tree) is a data structure used to perform
cumulative frequency and range sum queries efficiently. It allows for operations like
finding the sum of elements in a given range and updating an element.

Question 71
Which of the following is a correct way to implement a priority queue using a binary
heap?

A) Insert at the head and delete at the tail.


B) Insert at the tail and delete at the head.
C) Insert at the tail and delete at the tail.
D) Insert at the head and delete at the head.
Answer: B) Insert at the tail and delete at the head.
Explanation: In a binary heap, elements are inserted at the tail (the next available
position) and deleted from the head (the root), which contains the maximum or
minimum element, depending on whether it's a max heap or min heap.

Question 72
What is the time complexity of the build operation in a binary heap?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: The build operation in a binary heap has a time complexity of O(n)
because it involves creating the heap from the input array, which can be done in
linear time using a bottom-up approach.

Question 73
Which of the following is a true statement about depth-first search (DFS)?

A) It is a breadth-first traversal algorithm.


B) It is a depth-first traversal algorithm.
C) It is a sorting algorithm.
D) It is a searching algorithm.
Answer: B) It is a depth-first traversal algorithm.
Explanation: Depth-first search (DFS) is an algorithm for traversing or searching tree
or graph data structures. It starts at the root (selecting some arbitrary node as the
root in the case of a graph) and explores as far as possible along each branch before
backtracking.

Question 74
What is the purpose of a breadth-first search (BFS) in a graph?
A) To find the shortest path between two vertices.
B) To traverse the graph in a depth-first manner.
C) To sort the vertices of the graph.
D) To index the vertices of the graph.
Answer: A) To find the shortest path between two vertices.
Explanation: Breadth-first search (BFS) is used to find the shortest path between two
vertices in terms of the number of edges. It traverses the graph level by level.

Question 75
Which of the following is a correct way to implement a min heap?

A) The value of each node is greater than or equal to the value of its parent.
B) The value of each node is less than or equal to the value of its parent.
C) The value of each node is equal to the value of its parent.
D) The value of each node is unrelated to the value of its parent.
Answer: B) The value of each node is less than or equal to the value of its parent.
Explanation: In a min heap, the value of each node is less than or equal to the value
of its parent. This property ensures that the minimum element is always at the root of
the heap.

Question 76
What is the time complexity of the Floyd-Warshall algorithm for finding the
shortest path in a graph with n vertices?

A) O(n log n)
B) O(n^2)
C) O(n^3)
D) O(2^n)
Answer: C) O(n^3)
Explanation: The Floyd-Warshall algorithm has a time complexity of O(n^3) because
it involves three nested loops, each iterating over all vertices.

Question 77
Which of the following is a true statement about dynamic programming?

A) It is a divide and conquer approach.


B) It is a greedy approach.
C) It is an approach where the problem is solved by breaking it down into simpler
sub-problems.
D) It is an approach where the problem is solved by considering the global optimum
directly.
Answer: C) It is an approach where the problem is solved by breaking it down into
simpler sub-problems.
Explanation: Dynamic programming is an approach where the problem is solved by
breaking it down into simpler sub-problems and storing the results of sub-problems
to avoid recomputation.
Question 78
What is the purpose of a memoization technique in dynamic programming?

A) To store data efficiently


B) To sort data
C) To index data
D) To reuse the results of sub-problems
Answer: D) To reuse the results of sub-problems
Explanation: Memoization is a technique used in dynamic programming to store the
results of sub-problems so that they can be reused later, avoiding redundant
computations.

Question 79
Which of the following is a correct way to implement a max heap?

A) The value of each node is greater than or equal to the value of its parent.
B) The value of each node is less than or equal to the value of its parent.
C) The value of each node is equal to the value of its parent.
D) The value of each node is unrelated to the value of its parent.
Answer: A) The value of each node is greater than or equal to the value of its parent.
Explanation: In a max heap, the value of each node is greater than or equal to the
value of its parent. This property ensures that the maximum element is always at the
root of the heap.

Question 80
What is the time complexity of the Prim's algorithm for finding the minimum
spanning tree of a graph with n vertices and m edges?

A) O(n log n)
B) O(n^2)
C) O(m log n)
D) O(mn)
Answer: C) O(m log n)
Explanation: Prim's algorithm uses a priority queue to select the minimum weight
edge. The time complexity is O(m log n) because each edge is processed once, and
the priority queue operations (insert and extract-min) take O(log n) time.

Question 81
Which of the following is a true statement about the Knuth-Morris-Pratt (KMP)
algorithm?

A) It is a sorting algorithm.
B) It is a string matching algorithm.
C) It is a graph traversal algorithm.
D) It is a dynamic programming algorithm.
Answer: B) It is a string matching algorithm.
Explanation: The Knuth-Morris-Pratt (KMP) algorithm is a string matching algorithm
that searches for occurrences of a pattern within a text. It uses a partial match table
to avoid unnecessary comparisons.

Question 82
What is the purpose of a hash function in a hash table?

A) To store data efficiently


B) To sort data
C) To index data
D) To search data
Answer: C) To index data
Explanation: A hash function in a hash table is used to convert a key into an index
that can be used to store and retrieve data efficiently.

Question 83
Which of the following is a correct way to handle collisions in a hash table?

A) Linear probing
B) Chaining
C) Rehashing
D) All of the above
Answer: D) All of the above
Explanation: Collisions in a hash table can be handled using various techniques such
as linear probing, chaining, and rehashing. Each method has its own advantages and
disadvantages.

Question 84
What is the purpose of a bloom filter?

A) To store data efficiently


B) To sort data
C) To index data
D) To test whether an element is a member of a set
Answer: D) To test whether an element is a member of a set
Explanation: A bloom filter is a space-efficient probabilistic data structure used to
test whether an element is a member of a set. It allows for a small false positive
probability but no false negatives.

Question 85
Which of the following is a correct way to implement a queue using a singly linked
list?

A) Enqueue operation: Insert at the head. Dequeue operation: Delete at the head.
B) Enqueue operation: Insert at the tail. Dequeue operation: Delete at the head.
C) Enqueue operation: Insert at the head. Dequeue operation: Delete at the tail.
D) Enqueue operation: Insert at the tail. Dequeue operation: Delete at the tail.
Answer: B) Enqueue operation: Insert at the tail. Dequeue operation: Delete at the
head.
Explanation: In a queue implemented using a singly linked list, the enqueue operation
involves inserting an element at the tail of the list, and the dequeue operation involves
deleting an element from the head of the list.

Question 86
What is the time complexity of the insertion operation in a hash table with a good
hash function?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: A) O(1)
Explanation: With a good hash function, the insertion operation in a hash table can
be performed in O(1) time because it involves directly accessing the element's
location using the hash function.

Question 87
Which of the following is a true statement about the Boyer-Moore algorithm?

A) It is a sorting algorithm.
B) It is a string matching algorithm.
C) It is a graph traversal algorithm.
D) It is a dynamic programming algorithm.
Answer: B) It is a string matching algorithm.
Explanation: The Boyer-Moore algorithm is a string matching algorithm that searches
for occurrences of a pattern within a text. It uses a bad character rule and a good
suffix rule to skip comparisons.

Question 88
What is the purpose of a trie data structure?

A) To store data efficiently


B) To sort data
C) To index data
D) To store and retrieve strings efficiently
Answer: D) To store and retrieve strings efficiently
Explanation: A trie, also known as a prefix tree, is a data structure used to store and
retrieve strings efficiently. It is particularly useful for string matching and prefix
searches.

Question 89
What is the time complexity of the build operation in a trie?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: The build operation in a trie has a time complexity of O(n) because it
involves inserting each element into the trie, which takes O(1) time per element.

Question 90
Which of the following is a correct way to implement a stack using a queue?

A) Push operation: Enqueue the element. Pop operation: Dequeue all elements except
the last one and then dequeue the last one.
B) Push operation: Enqueue the element. Pop operation: Dequeue all elements and
then enqueue them again.
C) Push operation: Enqueue the element. Pop operation: Dequeue the first element.
D) Push operation: Enqueue the element. Pop operation: Dequeue all elements except
the first one and then dequeue the first one.
Answer: A) Push operation: Enqueue the element. Pop operation: Dequeue all
elements except the last one and then dequeue the last one.
Explanation: To implement a stack using a queue, you need to ensure that the last
element enqueued is the first one to be dequeued. This can be achieved by
dequeuing all elements except the last one and then dequeuing the last one.

Question 91
What is the purpose of a segment tree?

A) To store data efficiently


B) To sort data
C) To index data
D) To perform range queries and updates efficiently
Answer: D) To perform range queries and updates efficiently
Explanation: A segment tree is a data structure used to perform range queries and
updates over an array efficiently. It allows for operations like range sum, range
minimum/maximum, and range update.

Question 92
What is the time complexity of the update operation in a segment tree?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: B) O(log n)
Explanation: The update operation in a segment tree has a time complexity of O(log
n) because it involves updating the tree by propagating the change up the tree.

Question 93
Which of the following is a true statement about red-black trees?

A) They are a type of binary search tree.


B) They are a type of linked list.
C) They are a type of queue.
D) They are a type of stack.
Answer: A) They are a type of binary search tree.
Explanation: Red-black trees are a type of self-balancing binary search tree that
maintains balance after insertion and deletion operations.

Question 94
What is the purpose of a binary indexed tree (Fenwick tree)?

A) To store data efficiently


B) To sort data
C) To index data
D) To perform cumulative frequency and range sum queries efficiently
Answer: D) To perform cumulative frequency and range sum queries efficiently
Explanation: A binary indexed tree (Fenwick tree) is a data structure used to perform
cumulative frequency and range sum queries efficiently. It allows for operations like
finding the sum of elements in a given range and updating an element.

Question 95
What is the time complexity of the build operation in a binary indexed tree?

A) O(1)
B) O(log n)
C) O(n)
D) O(n log n)
Answer: C) O(n)
Explanation: The build operation in a binary indexed tree has a time complexity of
O(n) because it involves initializing the tree from the input array, which takes O(1) time
per element.

Question 96
Which of the following is a correct way to implement a priority queue using a binary
heap?

A) Insert at the head and delete at the tail.


B) Insert at the tail and delete at the head.
C) Insert at the tail and delete at the tail.
D) Insert at the head and delete at the head.
Answer: B) Insert at the tail and delete at the head.
Explanation: In a binary heap, elements are inserted at the tail (the next available
position) and deleted from the head (the root), which contains the maximum or
minimum element, depending on whether it's a max heap or min heap.

Question 97
What is the time complexity of the Prim's algorithm for finding the minimum
spanning tree of a graph with n vertices and m edges?

A) O(n log n)
B) O(n^2)
C) O(m log n)
D) O(mn)
Answer: C) O(m log n)
Explanation: Prim's algorithm uses a priority queue to select the minimum weight
edge. The time complexity is O(m log n) because each edge is processed once, and
the priority queue operations (insert and extract-min) take O(log n) time.

Question 98
Which of the following is a true statement about depth-first search (DFS)?

A) It is a breadth-first traversal algorithm.


B) It is a depth-first traversal algorithm.
C) It is a sorting algorithm.
D) It is a searching algorithm.
Answer: B) It is a depth-first traversal algorithm.
Explanation: Depth-first search (DFS) is an algorithm for traversing or searching tree
or graph data structures. It starts at the root (selecting some arbitrary node as the
root in the case of a graph) and explores as far as possible along each branch before
backtracking.

Question 99
What is the purpose of a breadth-first search (BFS) in a graph?

A) To find the shortest path between two vertices.


B) To traverse the graph in a depth-first manner.
C) To sort the vertices of the graph.
D) To index the vertices of the graph.
Answer: A) To find the shortest path between two vertices.
Explanation: Breadth-first search (BFS) is used to find the shortest path between two
vertices in terms of the number of edges. It traverses the graph level by level.

Question 100
Which of the following is a correct way to implement a min heap?

A) The value of each node is greater than or equal to the value of its parent.
B) The value of each node is less than or equal to the value of its parent.
C) The value of each node is equal to the value of its parent.
D) The value of each node is unrelated to the value of its parent.
Answer: B) The value of each node is less than or equal to the value of its parent.
Explanation: In a min heap, the value of each node is less than or equal to the value
of its parent. This property ensures that the minimum element is always at the root of
the heap.

You might also like