2 var
2 var
- O(1)
- O(n)
- O(logn)
- O(n^2)
2. Which condition must be true for binary search to work correctly on an array?
- The array must be sorted in ascending or descending order
- The array must be unsorted
- The array must have an odd number of elements
- The array must contain unique elements only
3. Which of the following operations can be performed in O(logn) time on a balanced Binary
Search Tree (BST)?
- Searching for an element
- Inserting an element
- Deleting an element
- All of the above
4. Which of the following is true about a binary heap used to implement a priority queue?
- A binary heap is always a complete binary tree
- A binary heap can be used to efficiently find the maximum (minimum) element in O(n) time
- In a min-heap, the parent node has a value greater than or equal to its children
- In a max-heap, the root is the smallest element
6. Suppose we have an O(n) time algorithm that finds the median of an unsorted array. Now
consider a QuickSort implementation where we first find the median using the above algorithm,
then use the median as a pivot.
What will be the worst-case time complexity of this modified QuickSort?
- O(n^2 logn)
- O(n^2)
- O(n logn logn)
- O(n logn)
8. Which of the following is a key feature of the Rabin-Karp algorithm for string matching?
- It uses a fixed-size pattern for hashing
- It performs character-by-character comparison for every match
- It utilizes a rolling hash function to efficiently compute substring hashes
- It always guarantees a worst-case time complexity of O(n)
9. What is the purpose of the prefix function in the Knuth-Morris-Pratt (KMP) algorithm?
- It helps to compare the pattern to every substring in the text
- It reduces the space complexity of the algorithm
- It indicates the longest proper prefix of the pattern that is also a suffix, allowing for skipping
unnecessary comparisons
- It ensures that the text is preprocessed before the matching phase
10. Which of the following is a typical application of the Breadth-First Search (BFS) algorithm?
- Finding the shortest path in a weighted graph
- Finding the shortest path in an unweighted graph
- Sorting the nodes of a graph
- Finding the maximum flow in a network
11. Which of the following statements about Depth-First Search (DFS) is NOT true?
- DFS can be implemented using either recursion or an explicit stack.
- DFS explores a node's neighbors before visiting any of the node's descendants.
- In DFS, a node is marked as visited when it is first encountered.
- DFS can be used to detect cycles in a directed graph.
12. Which of the following conditions is necessary for performing a topological sort on a graph?
- The graph must be undirected
- The graph must be directed acyclic (DAG)
- The graph must be fully connected
- The graph must contain at least one cycle
16. Which of the following is a correct use case for the Bellman-Ford algorithm?
- Finding the shortest path from a single source to all other vertices in a graph with positive
edge weights only.
- Finding the shortest path from a single source to all other vertices in a graph that contains
negative weight cycles.
- Detecting negative weight cycles in a graph.
- Finding the MST
18. Given a graph with weighted edges, where vertex 0 is the source, calculate the sum of the
shortest path distances from vertex 0 to all other vertices using Dijkstra's algorithm.
20. Use Breadth-First Search (BFS) to describe the sequence of nodes that would be visited
starting from node 1. Neighbors are discovered in ascending numerical order.
21. Given a binary heap, how many swaps are necessary to maintain the heap property after
inserting a node with the value 40?
Answer: 2
swaps
22. Use DFS to describe the sequence of nodes that would be visited starting from node a.
Neighbors are discovered in ascending numerical order.
23. Given the pattern "ababaa" for searching, what will be the Longest Proper Prefix (LPS) array
according to Knuth-Morris-Pratt (KMP) algorithm?
24. What will be the order of nodes visited by Prim's algorithm when finding the Minimum
Spanning Tree (MST), starting from node 0?