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

2 var

Uploaded by

qaq52841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2 var

Uploaded by

qaq52841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is the time complexity of accessing the n-th element in a doubly linked list?

- 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

5. What is the auxiliary space complexity of Heap Sort?


- O(n)
- O(logn)
- O(1)
- O(n logn)

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)

7. What is the primary advantage of Merge Sort over Quick Sort?


- Merge Sort is an in-place sorting algorithm.
- Merge Sort is stable, meaning it preserves the relative order of equal elements.
- Merge Sort has a worst-case time complexity of O(n).
- Merge Sort requires less memory.

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

13. Which of the following is a characteristic of a Minimum Spanning Tree (MST) of a


connected, weighted, undirected graph?
- The total weight of the MST is the maximum weight of all edges in the graph.
- The MST contains exactly V-1 edges, where V is the number of vertices in the graph.
- The MST is unique, regardless of the edge weights.
- The MST includes all the edges with the highest weight.

14. Which of the following is NOT a limitation of Dijkstra's algorithm?


- Dijkstra's algorithm cannot handle negative edge weights correctly.
- Dijkstra's algorithm finds the shortest path from a single source to all other vertices.
- Dijkstra's algorithm requires the graph to be connected to work properly.
- Dijkstra's algorithm is more efficient than brute force algorithms like Bellman-Ford for graphs
with no negative edge weights.

15. Which of the following is true about the Floyd-Warshall algorithm?


- It can handle negative edge weights but fails if there is a negative weight cycle.
- It only works on undirected graphs.
- It requires the graph to be connected in order to find the shortest paths.
- It computes the shortest paths from a single source vertex to all other vertices in the graph.

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

17. Consider a directed graph with nodes A, B, C, D, and edges:


A→B, A→C, B→D, C→E, E→D
What is a possible topological sort for this graph?

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.

19. What is the time complexity of the following code?


java
int a = 0, i = N;
while (i > 0) { Answer: O(logN)
a += i; i /= 2;
}

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?

You might also like