1. Which of the following is a stable sorting algorithm?
A. QuickSort
B. MergeSort
C. HeapSort
D. SelectionSort
Answer: b) MergeSort
Rationale: A stable sorting algorithm maintains the relative order of
records with equal keys (values). MergeSort is stable, while QuickSort,
HeapSort, and SelectionSort are not stable by default.
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)
Rationale: Binary search works by dividing the search space in half at
each step, which results in a logarithmic time complexity, O(log n),
when the input is sorted.
3. Which of the following data structures can be used to implement a
breadth-first search (BFS)?
A. Stack
B. Queue
C. Array
D. Linked List
Answer: b) Queue
Rationale: BFS explores nodes level by level, which is naturally
implemented using a queue. Nodes are processed in the order they are
discovered, which is a FIFO (First In First Out) structure, like a queue.
4. What is the worst-case time complexity of QuickSort?
,A. O(n)
B. O(n log n)
C. O(n^2)
D. O(log n)
Answer: c) O(n^2)
Rationale: QuickSort's worst-case time complexity is O(n^2) when the
pivot chosen is the smallest or largest element in the array, leading to
unbalanced partitions. However, its average case is O(n log n).
5. What is the primary disadvantage of using a linked list over an
array?
A. Random access is not possible
B. Linked lists require less memory
C. Insertion and deletion are slower
D. Arrays are more flexible
Answer: a) Random access is not possible
, Rationale: In a linked list, accessing an element requires traversing the
list from the beginning, so random access (directly accessing an index)
is not possible like it is in arrays.
6. In which of the following cases would you use a hash table?
A. When you need fast access to elements by key
B. When you need to maintain elements in sorted order
C. When you need to perform frequent deletions and insertions
D. When you need to implement recursion
Answer: a) When you need fast access to elements by key
Rationale: Hash tables provide average constant time complexity (O(1))
for access, insertion, and deletion operations by using a key to directly
access an element, making it ideal for fast lookups.
7. Which of the following algorithms is used to find the shortest path in
a graph with non-negative weights?
A. Bellman-Ford
A. QuickSort
B. MergeSort
C. HeapSort
D. SelectionSort
Answer: b) MergeSort
Rationale: A stable sorting algorithm maintains the relative order of
records with equal keys (values). MergeSort is stable, while QuickSort,
HeapSort, and SelectionSort are not stable by default.
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)
Rationale: Binary search works by dividing the search space in half at
each step, which results in a logarithmic time complexity, O(log n),
when the input is sorted.
3. Which of the following data structures can be used to implement a
breadth-first search (BFS)?
A. Stack
B. Queue
C. Array
D. Linked List
Answer: b) Queue
Rationale: BFS explores nodes level by level, which is naturally
implemented using a queue. Nodes are processed in the order they are
discovered, which is a FIFO (First In First Out) structure, like a queue.
4. What is the worst-case time complexity of QuickSort?
,A. O(n)
B. O(n log n)
C. O(n^2)
D. O(log n)
Answer: c) O(n^2)
Rationale: QuickSort's worst-case time complexity is O(n^2) when the
pivot chosen is the smallest or largest element in the array, leading to
unbalanced partitions. However, its average case is O(n log n).
5. What is the primary disadvantage of using a linked list over an
array?
A. Random access is not possible
B. Linked lists require less memory
C. Insertion and deletion are slower
D. Arrays are more flexible
Answer: a) Random access is not possible
, Rationale: In a linked list, accessing an element requires traversing the
list from the beginning, so random access (directly accessing an index)
is not possible like it is in arrays.
6. In which of the following cases would you use a hash table?
A. When you need fast access to elements by key
B. When you need to maintain elements in sorted order
C. When you need to perform frequent deletions and insertions
D. When you need to implement recursion
Answer: a) When you need fast access to elements by key
Rationale: Hash tables provide average constant time complexity (O(1))
for access, insertion, and deletion operations by using a key to directly
access an element, making it ideal for fast lookups.
7. Which of the following algorithms is used to find the shortest path in
a graph with non-negative weights?
A. Bellman-Ford