

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















































































Question 1. Which notation describes the lower bound of an algorithm’s running time for all sufficiently large inputs? A) Big O B) Big Omega C) Big Theta D) Little o Answer: B Explanation: Big Omega (Ω) provides a lower‑bound guarantee, meaning the algorithm will take at least that amount of time for large inputs. Question 2. The amortized time complexity of Python list’s append operation is: A) O(1) worst‑case B) O(1) amortized C) O(n) worst‑case D) O(log n) amortized Answer: B Explanation: When a Python list runs out of capacity it resizes (usually doubling). Over many appends the average cost per operation is constant, O(1) amortized. Question 3. In a recurrence T(n)=2T(n/2)+n, the Master Theorem gives a solution of: A) O(n) B) O(n log n) C) O(n²) D) O(log n) Answer: B
Explanation: Here a=2, b=2, f(n)=n. Since f(n)=Θ(n^{log_b a}) = Θ(n), we are in case 2 of the Master Theorem, giving Θ(n log n). Question 4. Which of the following best describes an Abstract Data Type (ADT)? A) A concrete class implementation in Python B) Only the data layout in memory C) A set of operations and their mathematical behavior, independent of implementation D) The source code of a data structure Answer: C Explanation: An ADT defines what operations are available and their contracts, not how they are realized. Question 5. In the context of ADTs, an invariant is: A) A function that changes the structure’s state B) A condition that must always hold true for the data structure’s correctness C) The time complexity of an operation D) The name of the ADT Answer: B Explanation: Invariants are properties that remain true before and after any operation, ensuring structural integrity. Question 6. The time complexity of slicing a Python list L[5:15] where the slice length is k is: A) O(1) B) O(k) C) O(n) where n = len(L)
D) Traversing forward through the list Answer: B Explanation: Searching requires visiting each node sequentially, O(n), regardless of single or double links. Question 10. A circular linked list is especially useful when: A) Random access by index is required B) You need a constant‑time lookup by key C) You want to easily rotate the list without moving nodes D) You need to store hierarchical data Answer: C Explanation: Because the last node points back to the first, you can rotate the logical start of the list in O(1) by moving a pointer. Question 11. Which of the following correctly describes the space complexity of a recursive depth‑first traversal of a binary tree with n nodes? A) O(1) B) O(log n) on a balanced tree, O(n) in the worst case C) O(n²) D) O(n log n) Answer: B Explanation: The recursion stack depth equals the tree height: O(log n) for balanced trees, O(n) for a completely skewed tree. Question 12. Which Python data structure provides O(1) average‑case time for push and pop at one end? A) list used as a stack (append/pop)
B) collections.deque used as a stack C) list used as a queue (insert(0)) D) set Answer: A Explanation: list.append and list.pop() at the end are O(1) amortized; they implement a stack efficiently. Question 13. In the classic “parentheses matching” problem, which data structure is most appropriate? A) Queue B) Stack C) Binary Search Tree D) Hash Table Answer: B Explanation: As you scan the string, you push opening symbols onto a stack and pop when a matching closing symbol appears. Question 14. When converting an infix expression to postfix using the Shunting‑Yard algorithm, the auxiliary data structure used to hold operators is a: A) Queue B) Stack C) List (used as array) D) Priority queue Answer: B Explanation: The algorithm pushes operators onto a stack to manage precedence and associativity.
Explanation: deque provides constant‑time operations at both ends. Question 18. Which of the following best describes a hash table’s load factor? A) Number of collisions per bucket B) Ratio of stored elements to table size C) Average length of a linked list in separate chaining D) Time taken to compute a hash value Answer: B Explanation: Load factor = n / m where n is number of entries and m is number of buckets; it determines when rehashing occurs. Question 19. In Python’s built‑in dict, when the load factor exceeds a threshold, the table is: A) Shrunk to half its size B) Rehashed into a larger table, typically doubling the size C) Left unchanged; Python never resizes dictionaries D) Converted into a list of key‑value pairs Answer: B Explanation: CPython grows the hash table (often doubling) to keep average‑case O(1) performance. Question 20. Which collision‑resolution technique uses linked lists (or other containers) stored at each bucket? A) Linear probing B) Quadratic probing C) Separate chaining
D) Double hashing Answer: C Explanation: Separate chaining places all elements that hash to the same bucket into a secondary container, commonly a linked list. Question 21. The average‑case time complexity of searching for a key in a Python set is: A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: A Explanation: set is implemented as a hash table, giving expected constant‑time lookup. Question 22. Which operation on a Python frozenset is NOT allowed? A) Membership test (in) B) Union (|) C) Adding a new element (add) D) Intersection (&) Answer: C Explanation: frozenset is immutable; you cannot add or remove elements after creation. Question 23. In a binary tree, the height of a leaf node is defined as: A) 0 B) 1 C) Equal to its depth
B) The heap property is restored by swapping upward until parent ≥ child C) All leaf nodes are sorted D) The array is re‑sorted using quicksort Answer: B Explanation: Sift‑up (bubble‑up) swaps the new node with its parent while it is larger, restoring the max‑heap invariant. Question 27. The time complexity of building a binary heap from an unsorted array of n elements using the heapify method is: A) O(n log n) B) O(n) C) O(log n) D) O(n²) Answer: B Explanation: The bottom‑up heapify runs in linear time because the work per level decreases geometrically. Question 28. Which of the following statements about Heapsort is true? A) It is a stable sorting algorithm B) It requires O(n) extra space C) Its worst‑case time complexity is O(n log n) D) It only works on linked‑list data structures Answer: C Explanation: Heapsort builds a heap (O(n)) and repeatedly extracts the max/min (O(log n) each), for total O(n log n). It is not stable and uses O(1) auxiliary space.
Question 29. In a binary search tree (BST), the inorder traversal yields: A) Nodes in pre‑order sequence B) Nodes in post‑order sequence C) Nodes in descending order D) Nodes in ascending order Answer: D Explanation: Inorder visits left subtree, node, then right subtree, producing sorted order for a BST. Question 30. Deleting a node with two children in a BST typically replaces the node with: A) Its left child B) Its right child C) Its inorder predecessor or successor D) The root of the tree Answer: C Explanation: To maintain BST property, you replace the node with the maximum node in its left subtree (predecessor) or the minimum in its right subtree (successor), then delete that leaf. Question 31. The worst‑case height of a BST that receives keys in sorted order is: A) log n B) n/ C) n D) √n Answer: C Explanation: Inserting sorted keys creates a degenerate (linked‑list‑like) tree of height n.
Question 35. The space complexity of an adjacency matrix for a graph with V vertices is: A) O(V) B) O(E) C) O(V²) D) O(V + E) Answer: C Explanation: A V × V matrix stores a boolean (or weight) for each possible edge, requiring Θ(V²) space. Question 36. For a sparse graph, the adjacency‑list representation is preferred because its space complexity is: A) O(V²) B) O(V + E) C) O(E²) D) O(V) Answer: B Explanation: Each vertex stores a list of its incident edges, yielding Θ(V + E) space, which is efficient when E ≪ V². Question 37. In Breadth‑First Search (BFS), the data structure used to keep track of the frontier is a: A) Stack B) Queue C) Priority queue D) Set
Answer: B Explanation: BFS explores vertices level by level, requiring FIFO order, which a queue provides. Question 38. The time complexity of BFS on a graph represented with adjacency lists is: A) O(V + E) B) O(V²) C) O(E log V) D) O(V log V) Answer: A Explanation: Each vertex is dequeued once, and each edge is examined once, giving Θ(V + E). Question 39. Dijkstra’s algorithm assumes all edge weights are: A) Negative or zero B) Positive (non‑negative) C) Arbitrary integers D) Prime numbers Answer: B Explanation: The algorithm relies on the fact that once a node’s shortest distance is finalized, it cannot be improved; this fails with negative weights. Question 40. Which of the following graph traversal algorithms can be implemented using a stack without recursion? A) BFS B) Dijkstra’s algorithm C) DFS D) Bellman‑Ford
C) O(max(m, n)) D) O(m · n) Answer: A Explanation: Union iterates over the smaller set and adds elements to the larger one, resulting in linear time relative to total elements. Question 44. In a binary heap stored in an array, the children of node at index i (zero‑based) are located at: A) 2i and 2i+ B) 2i+1 and 2i+ C) i/2 and i/2+ D) i‑1 and i+ Answer: B Explanation: For zero‑based arrays, left child = 2i+1, right child = 2i+2. Question 45. Which of the following best describes the “balance factor” of a node in an AVL tree? A) Height of left subtree minus height of right subtree B) Number of nodes in left subtree minus right subtree C) Depth of node from the root D) Difference between node’s key and its parent’s key Answer: A Explanation: Balance factor = height(left) – height(right); AVL trees enforce it to be – 1, 0, or +1. Question 46. In a graph, a “bridge” is an edge whose removal: A) Increases the number of connected components
B) Decreases the graph’s diameter C) Turns the graph into a DAG D) Creates a cycle Answer: A Explanation: A bridge (cut‑edge) disconnects the graph when removed, increasing component count. Question 47. The time complexity of searching for a key in a balanced binary search tree (e.g., AVL) with n nodes is: A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: B Explanation: Height of a balanced BST is Θ(log n); search follows a root‑to‑leaf path. Question 48. Which of the following is true about the “rotate‑left” operation in a red‑black tree? A) It swaps the colors of the rotated nodes B) It preserves the binary search property while moving a right child up C) It only applies when the node’s left child is red D) It reduces the tree height by two levels Answer: B Explanation: A left rotation makes the right child the new parent while keeping inorder ordering intact.
Explanation: Although occasional resizes cost O(k), over n pushes the total work is O(n), giving O(1) amortized per push. Question 52. In a singly linked list, the time to find the middle element using the “slow‑fast pointer” technique is: A) O(1) B) O(log n) C) O(n) D) O(n²) Answer: C Explanation: Both pointers traverse the list once; the fast pointer moves twice as fast, but total steps are linear. Question 53. Which of the following statements about Python’s list.sort() is correct? A) It uses quicksort with worst‑case O(n²) B) It is stable and runs in O(n log n) worst case (Timsort) C) It sorts in place with O(1) extra space always D) It cannot sort lists of custom objects Answer: B Explanation: CPython’s list.sort() implements Timsort, which is stable and guarantees O(n log n) worst‑case time. Question 54. The “two‑pointer” technique on a sorted array to find a pair that sums to a target runs in: A) O(1) time B) O(log n) time C) O(n) time
D) O(n²) time Answer: C Explanation: Pointers start at both ends and move inward at most n steps total, yielding linear time. Question 55. In a circular doubly linked list, removing the current node when you have a reference to it takes: A) O(n) time because you must traverse to find the predecessor B) O(1) time because both next and prev are directly accessible C) O(log n) time due to binary search of nodes D) O(n log n) time Answer: B Explanation: With direct access, you can adjust the neighboring nodes’ pointers in constant time. Question 56. Which of the following is NOT a valid property of a “complete binary tree” stored in an array? A) No gaps between indices of existing nodes B) All levels except possibly the last are completely filled C) The last level’s nodes are left‑justified D) Every node has exactly two children Answer: D Explanation: In a complete binary tree, some nodes (especially near the bottom) may have only one child. Question 57. The worst‑case number of comparisons required to search for a key in an unsorted array of n elements is: