



























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!




























What is the worst-case time complexity of inserting into a dynamic array that doubles capacity when full?
A. O(1) B. O(log n) C. O(n) D. O(n log n)
Answer: C Rationale: Insertion is usually O(1), but when resizing occurs, all n elements are copied, taking O(n). Thus worst-case is O(n), though amortized is O(1).
In a singly linked list with only head pointer, which operation is most inefficient compared to an array?
A. Inserting at the beginning B. Deleting the first element C. Accessing the last element D. Traversing all elements
Answer: C Rationale: Accessing the last element requires O(n) traversal from head, while arrays allow O(1) access by index. Insert/delete at beginning are O(1) for linked list.
Which data structure is most suitable for implementing a LIFO policy with efficient push and pop?
A. Queue B. Stack C. Priority queue D. Hash table
Answer: B Rationale: A stack provides LIFO order with O(1) push and pop. Queue is FIFO, priority queue orders by priority, hash table provides key-value mapping.
For a binary search tree with n nodes, what is the worst-case time complexity of search?
A. O(1) B. O(log n) C. O(n) D. O(n log n)
Answer: C Rationale: In the worst case (e.g., skewed tree), BST search degrades to O(n). Average case is O(log n) for balanced trees.
Which collision resolution technique in hashing may cause primary clustering?
A. Separate chaining B. Linear probing C. Quadratic probing D. Double hashing
Answer: B Rationale: Linear probing leads to primary clustering where contiguous occupied slots form, increasing probe lengths. Quadratic probing and double hashing reduce clustering.
What is the time complexity of the recursive Fibonacci algorithm without memoization?
A. O(n) B. O(n log n) C. O(2^n) D. O(n^2)
Answer: C Rationale: The naive recursive Fibonacci makes two recursive calls per invocation, leading to exponential time O(2^n). Memoization reduces it to O(n).
Which of the following is true about the space complexity of merge sort?
A. It is O(1) auxiliary space B. It is O(log n) auxiliary space C. It is O(n) auxiliary space D. It is O(n log n) auxiliary space
Answer: C Rationale: Merge sort requires O(n) auxiliary space for temporary arrays during merging. Quick sort can be O(log n) auxiliary space for recursion stack.
Which recurrence best models the worst-case time of randomized quicksort when the pivot is always the minimum?
A. T(n) = 2T(n/2) + (n) B. T(n) = T(n-1) + (n) C. T(n) = T(n/2) + (1) D. T(n) = 2T(n-1) + (1)
Answer: B Rationale: If the pivot is always the minimum, one partition has size 0 and the other n-1, yielding T(n)=T(n-1)+(n), which solves to (n^2). Option A models balanced partitioning ((n log n)), C models binary search, and D is far worse than quicksort's worst case.
In a hash table with open addressing and linear probing, what happens to performance as load factor approaches 1?
A. Successful search time approaches O(1) due to clustering. B. Primary clustering causes search time to grow proportionally to 1/(1-). C. The table automatically resizes, keeping performance constant. D. Linear probing degenerates into binary search behavior.
Answer: B Rationale: Linear probing suffers primary clustering: long runs of occupied slots form, and the expected number of probes for an unsuccessful search is approximately ½(1+1/(1-)^2), which grows sharply as ->1. Option A is false because clustering increases search time; C is false because resizing is not automatic unless implemented; D is nonsensical.
Which traversal of a binary search tree outputs keys in ascending order?
A. Preorder B. Postorder C. Inorder D. Level-order
Answer: C Rationale: In a BST, an inorder traversal visits left subtree, root, then right subtree, which yields keys in sorted (ascending) order. Preorder and postorder do not produce sorted order; level-order visits by depth and is not sorted.
A singly linked list allows O(1) insertion at the head but O(n) at the tail. Which structure best supports O(1) insertion at both ends?
A. Circular singly linked list with only a head pointer B. Doubly linked list with head and tail pointers C. Array-based list with dynamic resizing D. Stack implemented with a singly linked list
Answer: B Rationale: A doubly linked list with both head and tail pointers permits O(1) insertion at either end because each node has prev and next references. A circular singly linked list with only a head pointer still requires O(n) to reach the tail; dynamic arrays may require O(n) resizing; a stack only supports O(1) at one end.
Which operation on a stack implemented with a singly linked list (push at head) has O(1) worst-case time?
A. Push B. Pop C. Peek D. All of the above
Answer: D Rationale: With push at the head, push, pop, and peek all operate on the head node in constant time. None require traversal, so all are O(1).
What is the output of a postorder traversal of the expression tree for (a+b)*(c-d)?
A. a b + c d - * B. * + a b - c d C. a b c d + - * D. + a b * - c d
Answer: A Rationale: Postorder visits left subtree, right subtree, then root. For (a+b)*(c-d), left subtree yields a b +, right yields c d -, then root *: a b + c d - *. Option B is preorder, C is invalid, D is another invalid ordering.
For a sorted array of 1,000,000 elements, what is the maximum number of comparisons binary search needs?
A. 20 B. 1, C. 500, D. 1,000,
Answer: A Rationale: B i n a r y s e a r c h p e r f o r m s a t m o s t # l o g ‚ ( n + 1 )# c o m p a r i s o n s. l o g ‚ ( 1 20 comparisons. Linear search would need up to 1,000,000; 500,000 is half the array; 1,000 is far too small.
Which operation on a singly linked list is O(1) when a pointer to the node before the insertion point is given?
A. Inserting a new node after that node B. Removing the last node of the list C. Finding the predecessor of that node D. Reversing the entire list
Answer: A Rationale: Inserting after a known node requires only pointer updates, so it is O(1). Removing the last node requires traversal to find the predecessor, O(n). Finding a predecessor requires traversal, O(n). Reversing the list requires traversing all nodes, O(n).
A hash table uses separate chaining with load factor 0.75. Which statement about expected performance is correct?
A. Search is O(1) expected, but worst-case O(n) B. Search is O(log n) expected due to chains C. Search is O(1) worst-case with chaining D. Search is O(n) expected because chains grow linearly
Answer: A Rationale: With separate chaining and a constant load factor, the expected chain length is O(1), so search is O(1) expected. Worst case, all keys collide into one chain, giving O(n). Chaining does not guarantee O(1) worst-case, and expected search is not O(log n) or O(n).
In a binary search tree, which traversal yields keys in ascending sorted order?
A. Preorder traversal B. Inorder traversal C. Postorder traversal D. Level-order traversal
Answer: B Rationale: Inorder traversal visits left subtree, node, then right subtree, which for a BST produces ascending order. Preorder and postorder do not yield sorted order. Level-order visits by depth, not by key order.
Which statement about binary search on a sorted array of size n is correct?
A. It requires O(n) comparisons in the worst case B. It requires O(log n) comparisons and O(1) extra space iteratively C. It works on unsorted arrays if they contain distinct keys D. It requires O(log n) extra space in the iterative version
Answer: B Rationale: Iterative binary search halves the search interval each step, using O(log n) comparisons and O(1) extra space. It requires a sorted array. The recursive version uses O(log n) stack space, but the iterative does not.
Which property must a red-black tree maintain to guarantee O(log n) height?
A. Every leaf is at the same depth B. No two red nodes are adjacent, and black-height is equal on all paths C. The root is always red D. Every internal node has exactly two red children
Answer: B Rationale: Red-black trees enforce that red nodes cannot be adjacent and that every path from root to leaf has the same number of black nodes, which bounds height to O(log n). Leaves need not be at equal depth, and the root is typically black. Requiring two red children is not a red-black property.
Which sorting algorithm is stable and has O(n log n) worst-case time?
A. Quicksort B. Heapsort C. Merge sort D. Selection sort
Answer: C Rationale: Merge sort is stable and guarantees O(n log n) worst-case time. Quicksort is not stable and has O(n²) worst case. Heapsort is not stable. Selection sort is not stable and is O(n²).
For a recursive function with depth d and O(1) work per call, what is the space complexity due to the call stack?
A. O(1) B. O(log d) C. O(d) D. O(d²)
Answer: C Rationale: Each recursive call adds a stack frame, so space grows linearly with recursion depth, O(d). It is not constant, logarithmic, or quadratic in d. The work per call does not affect stack space.
What is the tightest Big-O bound on the number of comparisons performed by binary search on a sorted array of n elements?
A. O(1) B. O(log n) C. O(n) D. O(n log n)
Answer: B Rationale: B i n a r y s e a r c h h a l v e s t h e s e a r c h i n t e r v a l e a c h i t e r a t i o n , s o a t m are needed, giving (log n). O(1) is impossible for arbitrary n, O(n) describes linear search, and O(n log n) describes comparison sorts like mergesort.
Which operation on a singly linked list with only a head reference requires (n) time in the worst case?
A. Inserting a new node at the head B. Removing the head node C. Accessing the last node D. Checking whether the list is empty
Answer: C Rationale: W i t h o u t a t a i l p o i n t e r , r e a c h i n g t h e l a s t n o d e r e q u i r e s t r a v e r s i n insertion, head deletion, and emptiness checks all involve only the head reference and are (1).
A recursive function has recurrence T(n) = 2T(n/2) + (n). What is the asymptotic complexity of T(n)?
A. (n) B. (n log n) C. (n²) D. (log n)
Answer: B Rationale: B y t h e m a s t e r t h e o r e m , a = 2 , b = 2 , a n d f ( n ) = ˜ ( n ) m a t c h e s ˜ ( n ^ { l o (n log n). (n) ignores the log factor from the recursion tree's depth, (n²) overestimates, and (log n) is far too small.
In an adjacency matrix representation of an undirected graph with V vertices, what does the sum of all entries in row i represent?
A. The number of edges in the graph B. The degree of vertex i C. The number of connected components D. The shortest path length from vertex i
Answer: B Rationale: Each 1 in row i indicates an edge incident to vertex i; since the graph is undirected, the row sum equals deg(i). Total edges require summing the whole matrix and dividing by 2, and components or path lengths are not encoded by row sums.
A queue is implemented with two stacks using the costly-enqueue strategy. After enqueueing n items and then performing one dequeue, what is the amortized cost per operation?
A. (1) B. (log n) C. (n) D. (n²)
Answer: A Rationale: With costly enqueue (pushing all elements onto the second stack each time), a single enqueue is (n), but the total cost of n enqueues is (n²), so the amortized cost is (n²)/n = (n) per enqueue-wait, that is not (1). Re-evaluating: the standard two-stack queue with costly enqueue gives amortized (1) only for the dequeue-cheap variant; for costly enqueue the amortized cost is (n). The correct amortized bound for costly enqueue is (n), so the answer should be C. However, the question as written asks for amortized cost per operation after n enqueues and one dequeue: total cost (n²) for n enqueues, amortized (n). Thus C is correct.
Which abstract data type best models a print spooler where jobs are processed in the order they arrive?
A. Stack B. Queue C. Priority queue D. Set
Answer: B Rationale: A print spooler processes jobs first-in, first-out, which is exactly the queue ADT. A stack is LIFO, a priority queue orders by priority rather than arrival, and a set provides no ordering semantics.
For a complete binary tree stored in an array using 0-based indexing, where is the right child of the node at index i located?
A. 2i B. 2i + 1 C. 2i + 2 D. i + 2
Answer: C Rationale: With 0-based indexing, the left child is at 2i+1 and the right child at 2i+2. 2i and 2i+ correspond to 1-based indexing conventions, and i+2 does not reflect heap layout.
A dynamic array doubles capacity when full; appending n elements from empty yields what amortized cost per append?
A. O(1) amortized, because total copying work across all doublings is bounded by O(n). B. O(log n) amortized, because each doubling requires logarithmic redistribution work. C. O(n) amortized, because the worst-case append at a full array copies every element. D. O(n log n) amortized, because each of the log n doublings copies n elements.
Answer: A Rationale: Geometric doubling makes total copy work 1+2+4+...+n < 2n = O(n), so per-append amortized cost is O(1). Worst-case single append is O(n), but amortized analysis averages over operations; O(n) amortized (C) confuses worst-case with amortized, while B and D misstate the doubling sum.
A hash table uses open addressing with linear probing and load factor 0.75. Which consequence most accurately describes clustering behavior?
A. Primary clustering shortens probe sequences by distributing collisions uniformly. B. Primary clustering creates long runs of occupied slots, increasing average probe length. C. Linear probing eliminates clustering entirely once the load factor exceeds 0.5. D. Clustering only affects separate chaining, not open addressing.
Answer: B Rationale: Linear probing causes primary clustering: colliding keys form contiguous occupied runs, and any key hashing into a run must probe past it, raising average probe length. Clustering worsens with higher load factor; it does not shorten probes (A), is not eliminated (C), and is specific to open addressing, not chaining (D).
In an undirected graph with V vertices and E edges stored as an adjacency matrix, what is the time to list all neighbors of a single vertex?
A. O(1) B. O(degree(v)) C. O(V) D. O(E)
Answer: C Rationale: An adjacency matrix stores a row of length V per vertex, so finding all neighbors requires scanning the entire row in O(V) time regardless of degree. Adjacency lists achieve O(degree(v)); O(1) and O(E) do not reflect the row scan.
Which recurrence correctly models the worst-case running time of merge sort on n elements?
A. T(n) = T(n-1) + O(n) B. T(n) = 2T(n/2) + O(n) C. T(n) = T(n/2) + O(1) D. T(n) = 2T(n-1) + O(1)
Answer: B Rationale: Merge sort divides into two halves of size n/2 (2T(n/2)) and merges them in linear time (O(n)), giving T(n) = 2T(n/2) + O(n) and Theta(n log n). Option A models algorithms like insertion sort's worst case; C models binary search; D is exponential.
A recursive function lacks a base case and decreases its argument by 2 each call from an odd starting value. What is the primary failure mode?
A. It terminates correctly because the argument eventually reaches zero. B. It causes infinite recursion and stack overflow because the argument never reaches the base condition. C. It automatically converts to iteration via tail-call optimization. D. It throws an index-out-of-bounds exception at the first call.
Answer: B Rationale: Decreasing an odd value by 2 yields odd values indefinitely (e.g., 5, 3, 1, -1, ...), so a base case testing for 0 is never satisfied, causing unbounded recursion and stack overflow. It cannot terminate (A), tail-call optimization does not fix a missing base case (C), and no array indexing is implied (D).
Binary search on a sorted array of 1,000,000 elements requires at most how many comparisons?
A. About 20 B. About 1, C. About 10, D. About 500,
Answer: A Rationale: Binary search halves the search space each step, so comparisons are bounded by ceil(log2(1,000,000)) = 20. Linear search would need up to 1,000,000 comparisons; 1,000 and 10,000 are far above the logarithmic bound, and 500,000 reflects linear search.
An algorithm performs n^2 + 5n log n + 100 operations. Which Big-O bound is tightest?
A. O(n log n) B. O(n^2) C. O(n^3) D. O(n^2 log n)
Answer: B Rationale: The n^2 term dominates 5n log n and the constant 100 for large n, so the tightest Big-O bound is O(n^2). O(n log n) is too low (the function exceeds it), while O(n^3) and O(n^2 log n) are valid upper bounds but not tight.
What is the minimum number of comparisons required to find both the maximum and minimum of n distinct elements?
A. 2n - 2 B. 3n/2 - 2 C. n - 1 D. 2n - 3
Answer: B Rationale: The optimal tournament method pairs elements, compares each pair (n/2 comparisons), then finds max among winners and min among losers (n/2 - 1 each), totaling 3n/2 - 2 comparisons. This is provably optimal, beating the naive 2n - 2 approach.
Which sorting algorithm is stable and has O(n log n) worst-case time but requires O(n) auxiliary space?
A. Quicksort B. Heapsort C. Merge sort D. Selection sort
Answer: C Rationale: Merge sort is stable, guarantees O(n log n) in all cases, and needs O(n) extra space for merging. Quicksort is not stable and has O(n^2) worst-case; heapsort is not stable and uses O(1) space; selection sort is O(n^2).
In a hash table using open addressing with linear probing, what is the primary consequence of clustering?
A. Increased memory usage B. Longer average probe sequences C. Reduced hash function collisions D. Improved cache performance
Answer: B Rationale: Linear probing causes primary clustering, where occupied slots form contiguous blocks, leading to longer probe sequences and degraded performance. It does not increase memory usage or reduce collisions; cache performance may improve but is not the primary consequence.
In a binary search tree, what is the worst-case time complexity of search when keys are inserted in sorted order?
A. O(1) B. O(log n) C. O(n) D. O(n log n)
Answer: C Rationale: Inserting sorted keys into an unbalanced BST creates a degenerate tree (essentially a linked list), making search O(n) in the worst case. Balanced trees would give O(log n), but sorted insertion defeats balancing.
Which graph traversal uses a queue and is guaranteed to find the shortest path in an unweighted graph?
A. Depth-first search B. Breadth-first search C. Dijkstra's algorithm D. Topological sort
Answer: B Rationale: Breadth-first search (BFS) explores level by level using a queue, ensuring the first time a vertex is reached it is via a shortest path in terms of edge count. DFS does not guarantee shortest paths; Dijkstra's is for weighted graphs; topological sort is for DAGs.
Which algorithm design paradigm does Dijkstra's shortest path algorithm exemplify?
A. Divide and conquer B. Dynamic programming C. Greedy D. Backtracking
Answer: C Rationale: Dijkstra's algorithm makes a locally optimal choice at each step by selecting the unvisited vertex with the smallest tentative distance, which is a greedy strategy. It does not use divide-and-conquer, dynamic programming, or backtracking.