



















































































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 of the following best describes the greedy‑choice property? A) The algorithm explores all possible solutions before picking one. B) The algorithm makes a locally optimal choice that can lead to a globally optimal solution. C) The algorithm backs up and tries alternative choices when a dead end is reached. D) The algorithm guarantees the optimal solution only for NP‑complete problems. Answer: B Explanation: The greedy‑choice property requires that a locally optimal decision at each step can be part of some optimal solution. Question 2. In the activity‑selection problem, which sorting key yields an optimal schedule when using a greedy algorithm? A) Earliest start time B) Shortest duration C) Latest finish time D) Highest profit Answer: C Explanation: Sorting activities by increasing finish time ensures the maximum number of non‑overlapping activities. Question 3. Which data structure is most appropriate for implementing the priority queue in Prim’s algorithm to achieve O(E log V) time? A) Simple array B) Binary min‑heap C) Stack
D) Linked list Answer: B Explanation: A binary min‑heap supports extract‑min and decrease‑key in logarithmic time, giving the stated complexity. Question 4. The cut property of MSTs states that: A) The heaviest edge in any cut belongs to every MST. B) The lightest edge crossing any cut is in some MST. C) Any edge crossing a cut can be removed without affecting the MST. D) All edges in a cut have equal weight in an MST. Answer: B Explanation: For any cut, the minimum‑weight edge crossing it (the light edge) is safe to include in at least one MST. Question 5. In Kruskal’s algorithm, what is the purpose of the Union‑Find (Disjoint Set Union) structure? A) To sort the vertices by degree. B) To keep track of which vertices belong to the same component, preventing cycles. C) To compute shortest paths from the source. D) To store the final MST edges in a balanced tree. Answer: B Explanation: Union‑Find quickly determines whether adding an edge would create a cycle by checking component membership.
Explanation: Distinct edge weights guarantee that the light edge for every cut is unique, leading to a single MST. Question 9. In the job‑sequencing with deadlines problem, which greedy rule yields maximum profit? A) Schedule jobs in decreasing order of profit, placing each in the latest available slot before its deadline. B) Schedule jobs in increasing order of deadline, ignoring profit. C) Schedule jobs with the shortest processing time first. D) Schedule jobs randomly and then improve by local swaps. Answer: A Explanation: Placing the most profitable jobs as late as possible (but before deadlines) preserves earlier slots for other jobs. Question 10. Which of the following is NOT a requirement for a problem to be solvable by dynamic programming? A) Optimal substructure B) Overlapping subproblems C) Polynomial‑time greedy choice property D) Ability to store intermediate results Answer: C Explanation: Greedy choice property is unrelated to DP; DP relies on optimal substructure and overlapping subproblems. Question 11. The recurrence relation for the Fibonacci sequence in a top‑down DP (memoization) approach is:
A) F(n) = F(n‑1) + F(n‑2) with base cases F(0)=0, F(1)=1. B) F(n) = 2·F(n‑1) with base case F(1)=1. C) F(n) = F(n‑1)·F(n‑2) with base cases F(0)=1, F(1)=1. D) F(n) = F(n‑3) + F(n‑4) with base cases F(0)=0, F(1)=1. Answer: A Explanation: The classic Fibonacci recurrence adds the two previous numbers; memoization caches each computed value. Question 12. In the 0/1 knapsack DP table, the entry DP[i][w] represents: A) The maximum profit using the first i items with exact weight w. B) The minimum weight needed to achieve profit w using i items. C) The maximum profit using the first i items with capacity w. D) The number of ways to fill weight w with i items. Answer: C Explanation: DP[i][w] stores the best value achievable with the first i items and knapsack capacity w. Question 13. Which of the following best describes the “exchange argument” used in greedy‑algorithm proofs? A) Showing that swapping two arbitrary elements never changes the solution quality. B) Demonstrating that any optimal solution can be transformed into the greedy solution without decreasing its value. C exchange argument is only used for sorting algorithms. D) Proving that the greedy algorithm runs in linear time. Answer: B
D) The edge that connects the two most distant vertices. Answer: B Explanation: A light edge is the minimum‑weight edge crossing a specific cut; it is safe to add to an MST. Question 17. Which of the following DP problems can be solved with a one‑dimensional array (i.e., DP[i])? A) Longest Common Subsequence for two strings. B) Matrix Chain Multiplication. C) Rod Cutting. D) Edit Distance. Answer: C Explanation: Rod cutting only depends on the length of the remaining rod, allowing a 1‑D DP table. Question 18. In Prim’s algorithm, after adding a vertex to the growing MST, which edges are examined next? A) All edges incident to the newly added vertex that lead to vertices not yet in the MST. B) All edges of the graph regardless of their endpoints. C) Only the edges that form a cycle with the current MST. D) No edges; the algorithm terminates after the first vertex. Answer: A Explanation: Prim’s algorithm expands the tree by considering edges from the frontier (vertices already in the MST to those outside).
Question 19. The “cycle property” of MSTs states that: A) Every cycle in a graph must contain at least one edge of the MST. B) The heaviest edge in any cycle cannot belong to any MST. C) All edges in a cycle have equal weight in an MST. D) Adding any edge to an MST creates exactly one cycle. Answer: B Explanation: Removing the maximum‑weight edge from any cycle never increases the total weight, so that edge is never in an MST. Question 20. Which recurrence correctly expresses the DP solution for the coin‑change problem that counts the number of ways to make amount n using unlimited coins of denominations {c₁,…,cₖ}? A) ways[n] = Σ ways[n‑cᵢ] for all i where n‑cᵢ ≥ 0, with ways[0]=1. B) ways[n] = minᵢ { ways[n‑cᵢ] + 1 } for all i where n‑cᵢ ≥ 0, with ways[0]=0. C) ways[n] = maxᵢ { ways[n‑cᵢ] } for all i where n‑cᵢ ≥ 0, with ways[0]=1. D) ways[n] = ways[n‑1] + ways[n‑2] for all n ≥ 2. Answer: A Explanation: The total number of ways to form n is the sum over all choices of the first coin, reducing the problem to n‑cᵢ. Question 21. In the matrix‑chain multiplication DP, the cost of multiplying matrices Aᵢ…Aⱼ is given by: A) DP[i][j] = minₖ { DP[i][k] + DP[k+1][j] + pᵢ₋₁·p_k·pⱼ } B) DP[i][j] = DP[i][j‑1] + pᵢ₋₁·pⱼ·pⱼ₊₁ C) DP[i][j] = maxₖ { DP[i][k]·DP[k+1][j] }
Question 24. In the activity‑selection greedy algorithm, after selecting an activity that finishes at time f, the next activity considered must: A) Start after time f. B) Start before time f. C) Have the shortest duration among remaining activities. D) Have the highest profit among remaining activities. Answer: A Explanation: To avoid overlap, the next chosen activity must begin no earlier than the finish time of the previously selected one. Question 25. Which of the following is a correct base case for the DP solution of the Longest Common Subsequence (LCS) problem? A) LCS[i][0] = i for all i. B) LCS[0][j] = 0 for all j. C) LCS[i][j] = 1 when i = j. D) LCS[i][j] = i + j when characters differ. Answer: B Explanation: An empty string versus any prefix of the other yields an LCS length of 0. Question 26. The “exchange argument” proof for the activity‑selection problem shows that: A) Any optimal schedule can be transformed to the greedy schedule by repeatedly swapping earlier‑finishing activities. B) The greedy schedule is always shorter than any optimal schedule. C) Swapping two overlapping activities never changes the total count.
D) The greedy algorithm fails when activities have equal finish times. Answer: A Explanation: By exchanging activities in an optimal solution with earlier‑finishing ones, we can obtain the greedy schedule without reducing the number of activities. Question 27. In the context of DP, what does “memoization” refer to? A) Storing all subproblem results in a table before any computation. B) Caching results of recursive calls as they are computed to avoid recomputation. C) Sorting subproblems in decreasing order of size. D) Using a priority queue to select subproblems. Answer: B Explanation: Memoization is a top‑down approach where each recursive call’s result is saved for future reuse. Question 28. When applying Kruskal’s algorithm, if the graph contains multiple edges with the same weight, the algorithm: A) May produce different MSTs depending on the order of processing equal‑weight edges. B) Always fails to produce an MST. C) Requires a tie‑breaking rule that guarantees uniqueness. D) Discards all duplicate‑weight edges automatically. Answer: A Explanation: With equal‑weight edges, any order that respects the “no‑cycle” rule yields a valid MST; multiple distinct MSTs may exist. Question 29. Which of the following is NOT a typical step in designing a DP algorithm?
Explanation: Distinct weights guarantee a unique light edge for every cut, leading to a single MST. Question 32. In the DP solution to the rod‑cutting problem, the recurrence relation is: A) R[n] = max₁≤i≤n { price[i] + R[n‑i] } with R[0]=0. B) R[n] = min₁≤i≤n { price[i] * R[n‑i] } with R[0]=1. C) R[n] = price[n] + R[n‑1]. D) R[n] = sum of all price[i] for i ≤ n. Answer: A Explanation: For each possible first cut i, we add its price and solve the subproblem of length n‑i, taking the maximum. Question 33. Which of the following problems typically requires a DP solution rather than a greedy one because the greedy choice fails? A) Minimum spanning tree. B) Fractional knapsack. C) 0/1 knapsack. D) Huffman coding. Answer: C Explanation: The 0/1 knapsack does not have the greedy‑choice property; DP is needed to consider all combinations. Question 34. In Prim’s algorithm, if the graph is represented by an adjacency matrix, the overall time complexity becomes: A) O(V²)
B) O(E log V) C) O(V log V + E) D) O(E + V) Answer: A Explanation: Scanning a V × V matrix to find the minimum edge each iteration yields O(V²) time. Question 35. Which of the following is a correct description of the “dark edge” in the cycle property of MSTs? A) The lightest edge in a cycle. B) The heaviest edge in a cycle. C) Any edge that belongs to a cut. D) An edge that connects two leaves of the MST. Answer: B Explanation: The “dark edge” refers to the maximum‑weight edge in a cycle, which can be safely removed from any MST. Question 36. In the DP formulation of the coin‑change problem that minimizes the number of coins, the recurrence is: A) minCoins[n] = 1 + min_{i}( minCoins[n‑cᵢ] ) for all cᵢ ≤ n, with minCoins[0]=0. B) minCoins[n] = Σ{i} minCoins[n‑cᵢ] for all cᵢ ≤ n. C) minCoins[n] = max{i}( minCoins[n‑cᵢ] ) + 1. D) minCoins[n] = minCoins[n‑1] + 1. Answer: A Explanation: To achieve amount n, we add one coin of denomination cᵢ and solve the subproblem for n‑cᵢ, taking the minimum over all viable coins.
C) It requires solving a linear programming relaxation first. D) It fails when two jobs share the same deadline. Answer: B Explanation: The algorithm focuses on profit, possibly leaving earlier slots empty to accommodate higher‑profit jobs later. Question 40. In a DP table for the 0/1 knapsack problem, why is the table typically built using a reverse iteration over the weight dimension when using a 1‑D array? A) To ensure each item is considered only once (prevent reuse). B) To sort items by weight. C) To improve cache locality. D) To guarantee the table remains sorted. Answer: A Explanation: Iterating weight from high to low prevents the current item’s contribution from being used multiple times within the same iteration. Question 41. Which of the following is a necessary condition for a greedy algorithm to be optimal for a given problem? A) The problem must have a polynomial‑time solution. B) The problem must satisfy both the greedy‑choice property and optimal substructure. C) The problem must be NP‑complete. D) The problem must have a unique solution. Answer: B Explanation: Both properties are required for greedy optimality.
Question 42. In the context of MSTs, a “cut” of a graph is defined as: A) A set of edges whose removal disconnects the graph. B) A partition of the vertices into two non‑empty disjoint subsets. C) A cycle that includes all vertices. D) A spanning tree of minimum weight. Answer: B Explanation: A cut separates the vertex set into two groups; edges crossing the cut are considered in the cut property. Question 43. Which of the following DP problems can be solved in O(n log n) time using a clever transformation? A) Longest Increasing Subsequence (LIS) B) Matrix Chain Multiplication C) 0/1 Knapsack D) Rod Cutting Answer: A Explanation: LIS can be solved in O(n log n) using patience sorting or binary search over tails. Question 44. In the greedy algorithm for the fractional knapsack problem, the ratio used for sorting is: A) weight/value B) value/weight C) value − weight D) value + weight Answer: B
B) fib[0] = 0, fib[1] = 1 C) fib[1] = 0, fib[2] = 1 D) fib[2] = 2, fib[3] = 3 Answer: B Explanation: The standard Fibonacci base values are 0 and 1. Question 48. In the activity‑selection problem, if two activities have the same finish time, which tie‑breaking rule preserves optimality? A) Choose the one with earlier start time. B) Choose the one with later start time. C) Choose arbitrarily; both lead to optimal solutions. D) Choose the one with longer duration. Answer: C Explanation: When finish times are equal, any choice maintains the ability to schedule the maximum number of activities. Question 49. Which of the following DP problems can be solved using a “state compression” technique to reduce memory usage? A) Traveling salesman problem (TSP) with bitmask DP B) Longest Common Subsequence C) Coin change (minimum coins) D) Rod cutting Answer: A Explanation: TSP uses DP[mask][i]; the mask compresses the subset of visited cities into a single integer.
Question 50. In Prim’s algorithm, the set of vertices already included in the MST is often called the: A) Frontier set B) Closed set C) Tree set D) Visited set Answer: C Explanation: The vertices that have been added to the growing MST are commonly referred to as the tree set. Question 51. Which of the following problems demonstrates that a greedy algorithm can be optimal even when edge weights are not distinct? A) Minimum spanning tree on a graph with equal edge weights B) Fractional knapsack with duplicate ratios C) Activity selection with overlapping intervals D) 0/1 knapsack with equal values Answer: A Explanation: Even with equal weights, any MST produced by Kruskal or Prim is still optimal (though not unique). Question 52. In the DP solution for the coin‑change problem that counts the number of ways, why must the outer loop iterate over coin denominations and the inner loop over amounts? A) To avoid counting permutations of the same combination multiple times. B) To ensure the algorithm runs in O(n²) time.