Subject: Design and Analysis of Algorithms (DAA)
Topics Included: Sorting Algorithms (Insertion Sort, Merge Sort, Quick Sort), Searching (Binary Search, Linear Search), Divide-and-Conquer (Closest Pair, Maximum Subarray), Recurrences & Master Theorem, Asymptotic Notation ($O, \Omega, \Theta, o$), and Loop Invariants.
Complete Quiz Questions, Answer Key & Explanations
Question 1
Question: Proving that an algorithm terminates is sufficient to prove that it produces the correct output. - a. True - b. False
Answer: b. False
Explanation:
In algorithm theory, proving algorithm correctness requires two independent components:
1. Partial Correctness: Proving that if the algorithm halts, it produces the correct output.
2. Termination: Proving that the algorithm halts in finite time for all valid inputs.
Proving termination alone is insufficient; an algorithm could terminate immediately returning an arbitrary incorrect constant (e.g., returning 0 for every input) without actually solving the problem.
Question 2
Question: Binary search is applied to a sorted array of 1,023 elements. What is the maximum number of comparisons required by the usual midpoint-based search? - a. 31 - b. 8 - c. 10 - d. 12
Answer: c. 10
Explanation:
The maximum number of comparisons performed by Binary Search on $n$ elements corresponds to the height of its decision tree:
$$\text{Max Comparisons} = \lfloor \log_2 n \rfloor + 1$$
For $n = 1023$, since $2^9 = 512 \le 1023 < 1024 = 2^{10}$:
$$\lfloor \log_2 1023 \rfloor + 1 = 9 + 1 = \mathbf{10}$$
Question 3
Question: Suppose insertion sort is applied to an array in which exactly $k$ elements are out of place, while all other elements are already in their correct relative order. Which statement best describes its performance? - a. Its running time depends only on the maximum element. - b. Its running time is always $\Theta(n \log n)$. - c. Its running time is always $\Theta(n)$, regardless of $k$. - d. Its running time depends on the number of inversions and is $\Theta(n+I)$, where $I$ is the number of inversions.
Answer: d. Its running time depends on the number of inversions and is $\Theta(n+I)$, where $I$ is the number of inversions.
Explanation:
An inversion is a pair of indices $(i, j)$ such that $i < j$ but $A[i] > A[j]$. Each inner-loop shift in Insertion Sort eliminates exactly one inversion. The algorithm performs $\Theta(n)$ work to iterate through the array plus work directly proportional to the total number of inversions $I$, yielding a runtime of $\Theta(n + I)$.
Question 4
Question: Which statement correctly describes the maintenance step of a loop-invariant proof? - a. It proves that the invariant holds before the loop starts. - b. It proves that if the invariant holds before an iteration, it also holds after that iteration. - c. It proves the algorithm terminates for every possible input. - d. It establishes the lower bound on the running time.
Answer: b. It proves that if the invariant holds before an iteration, it also holds after that iteration.
Explanation:
A loop-invariant proof mirrors mathematical induction and consists of three steps:
1. Initialization: The invariant is true prior to the first loop iteration.
2. Maintenance: If the invariant is true before an iteration, it remains true before the next iteration (i.e., after completing the current iteration).
3. Termination: When the loop terminates, the invariant provides a useful property that proves algorithm correctness.
Question 5
Question: Given $n$ points in the plane, what is the worst-case time complexity of the standard divide-and-conquer closest-pair algorithm when the initial sorting and strip processing are implemented efficiently? - a. $\Theta(n \log n)$ - b. $\Theta(n^2 \log n)$ - c. $\Theta(n^2)$ - d. $\Theta(n)$
Answer: a. $\Theta(n \log n)$
Explanation:
The 2D Closest-Pair divide-and-conquer algorithm recursively finds the minimum distance $\delta = \min(\delta_L, \delta_R)$ in the left and right halves. In the combine step, points within the central $2\delta$ strip are sorted by $y$-coordinate, where each point needs to be compared against at most 7 to 8 neighboring points. This keeps the combine step linear ($\Theta(n)$), yielding the recurrence:
$$T(n) = 2T(n/2) + O(n) \implies T(n) = \mathbf{\Theta(n \log n)}$$
Question 6
Question: Binary search can be applied correctly to a sorted linked list with the same $\Theta(\log n)$ running time as binary search on an array. - a. True - b. False
Answer: b. False
Explanation:
Array-based Binary Search achieves $\Theta(\log n)$ time because arrays support $O(1)$ random access to the middle element. Linked lists do not support random access; finding the middle element requires traversing $n/2$ pointers in $O(n)$ time. The resulting recurrence is $T(n) = T(n/2) + O(n) = \Theta(n)$, failing to achieve logarithmic runtime.
Question 7
Question: Consider a linear search for a key in an array of $n$ distinct elements. If the key is equally likely to be at any position, and it is guaranteed to be present, what is the expected number of comparisons? - a. $(n+1)/2$ - b. $n \log n$ - c. $n-1$ - d. $\log n$
Answer: a. $(n+1)/2$
Explanation:
Under a uniform distribution where the key is equally likely at any index $i \in {1, 2, \dots, n}$ with probability $p_i = 1/n$:
$$E[\text{comparisons}] = \sum_{i=1}^n i \cdot P(i) = \frac{1}{n} \sum_{i=1}^n i = \frac{1}{n} \cdot \frac{n(n+1)}{2} = \mathbf{\frac{n+1}{2}}$$
Question 8
Question: A recursive algorithm is claimed to compute $n!$ using the recurrence $F(n) = n \cdot F(n-1)$. Which additional condition is essential for a standard correctness proof? - a. The algorithm must run in $O(n \log n)$ time. - b. The recursion depth must be exactly $\log n$. - c. A correct base case must be established. - d. The algorithm must use a loop.
Answer: c. A correct base case must be established.
Explanation:
Proving the correctness of a recurrence via induction requires both an inductive step ($F(n) = n \cdot F(n-1)$) and a base case (e.g., $F(0) = 1$ or $F(1) = 1$). Without a base case, recursion continues infinitely without a termination condition.
Question 9
Question: An insertion-sort implementation shifts elements only while $(A[j] > \text{key})$, not while $(A[j] \ge \text{key})$. What is the consequence when duplicate keys occur? - a. The algorithm becomes quadratic even in the best case. - b. The algorithm becomes unstable. - c. The algorithm remains stable. - d. The algorithm no longer sorts correctly.
Answer: c. The algorithm remains stable.
Explanation:
Using strict inequality $A[j] > \text{key}$ ensures that when an element equal to $\text{key}$ is encountered during shifting, the loop terminates. The $\text{key}$ is inserted immediately after the existing equal element, maintaining their original relative input order and preserving stability.
Question 10
Question: Merge sort and insertion sort are both stable when implemented using their standard stable versions. - a. True - b. False
Answer: a. True
Explanation:
Standard implementations of Merge Sort (which takes elements from the left subarray first when $L[i] \le R[j]$) and Insertion Sort (which stops shifting when encountering an equal element) preserve the relative order of duplicate keys, making both algorithms inherently stable.
Question 11
Question: Insertion sort performs $\Theta(n^3)$ work on every input of size $n$, regardless of the initial order. - a. True - b. False
Answer: b. False
Explanation:
Insertion Sort has a worst-case time complexity of $\Theta(n^2)$ (reverse-sorted array) and a best-case time complexity of $\Theta(n)$ (already-sorted array). It never performs $\Theta(n^3)$ work.
Question 12
Question: Consider the array $A=[5,2,4,6,1,3]$. After the third iteration of the outer loop of standard insertion sort in ascending order, what is the array? - a. $[2,4,5,6,1,3]$ - b. $[1,2,4,5,6,3]$ - c. $[2,4,5,6,1,3]$ - d. $[2,5,4,6,1,3]$
Answer: c. $[2,4,5,6,1,3]$
Explanation:
Tracing the outer loop step-by-step:
- Initial: $[5, 2, 4, 6, 1, 3]$
- Iteration 1 ($\text{key}=2$): Inserts 2 before 5 $\to [2, 5, 4, 6, 1, 3]$
- Iteration 2 ($\text{key}=4$): Inserts 4 between 2 and 5 $\to [2, 4, 5, 6, 1, 3]$
- Iteration 3 ($\text{key}=6$): Since $6 > 5$, no elements shift $\to \mathbf{[2, 4, 5, 6, 1, 3]}$
Question 13
Question: For the array $A = [-5, -2, -8, -1, -6]$, what is the maximum contiguous subarray sum if the subarray must be nonempty? - a. -2 - b. -1 - c. 5 - d. 0
Answer: b. -1
Explanation:
When all elements in an array are negative, any contiguous subarray with 2 or more elements yields a smaller sum than taking the single maximum negative number. Thus, the non-empty maximum subarray sum is $\max(-5, -2, -8, -1, -6) = \mathbf{-1}$.
Question 14
Question: Consider deterministic quicksort that always selects the first element as the pivot. For a strictly increasing array, its partition procedure repeatedly produces subarrays of sizes $0$ and $n-1$. What is the total running time? - a. $\Theta(n \log n)$ - b. $\Theta(n)$ - c. $\Theta(\log n)$ - d. $\Theta(n^2)$
Answer: d. $\Theta(n^2)$
Explanation:
Choosing the first element on a sorted array picks the minimum element at every step, creating a maximally unbalanced split ($0$ and $n-1$ elements). The recurrence relation is:
$$T(n) = T(n-1) + \Theta(n) = \mathbf{\Theta(n^2)}$$
Question 15
Question: Suppose $f(n) = O(n^2)$ and $g(n) = \Omega(n^3)$, where both functions are nonnegative for sufficiently large $n$. Which of the following conclusions is necessarily true? - a. $f(n) = \Theta(g(n))$ - b. $g(n) = O(f(n))$ - c. $f(n) = o(g(n))$ - d. $f(n) = \Omega(g(n))$
Answer: c. $f(n) = o(g(n))$
Explanation:
By definition, $f(n) \le c_1 n^2$ and $g(n) \ge c_2 n^3$. Evaluating the ratio as $n \to \infty$:
$$\lim_{n \to \infty} \frac{f(n)}{g(n)} \le \lim_{n \to \infty} \frac{c_1 n^2}{c_2 n^3} = \lim_{n \to \infty} \frac{c_1}{c_2 n} = 0$$
Since the limit equals $0$, by formal definition of little-o notation, $f(n) = o(g(n))$.
Question 16
Question: Given the array $[-4, 3, -2, 5, -1, 2, -6, 4]$ (divide-and-conquer max subarray), which statement is correct? - a. It avoids recursively solving either half. - b. Any subarray crossing the midpoint consists of a suffix of the left half and a prefix of the right half. - c. Every maximum subarray must contain the first element. - d. The two halves must contain equal numbers of positive elements.
Answer: b. Any subarray crossing the midpoint consists of a suffix of the left half and a prefix of the right half.
Explanation:
Any contiguous subarray $A[i \dots j]$ that spans across the midpoint $mid$ ($i \le mid < j$) consists of two connected pieces: $A[i \dots mid]$ (which ends at $mid$, making it a suffix of the left half) and $A[mid+1 \dots j]$ (which begins at $mid+1$, making it a prefix of the right half).
Question 17
Question: The divide-and-conquer maximum-subarray algorithm can solve the problem correctly by considering only the maximum subarray entirely contained in the left half and the maximum subarray entirely contained in the right half. - a. True - b. False
Answer: b. False
Explanation:
The algorithm must evaluate three potential candidates:
1. Maximum subarray entirely in the left half.
2. Maximum subarray entirely in the right half.
3. Maximum crossing subarray spanning across the midpoint.
Omitting the crossing subarray fails to find contiguous subarrays that cross between the left and right halves.
Question 18
Question: An array is sorted in ascending order, but it contains many duplicate keys. Which statement about binary search is correct? - a. Binary search can find an occurrence, but additional logic may be needed to find the first occurrence. - b. Binary search always returns the first occurrence of a duplicate key. - c. Binary search is invalid whenever duplicates exist. - d. Binary search becomes order of n, whenever duplicates exist.
Answer: a. Binary search can find an occurrence, but additional logic may be needed to find the first occurrence.
Explanation:
Standard Binary Search terminates immediately when A[mid] == key, returning an arbitrary match index. To find the first occurrence, the algorithm must record mid as a candidate and continue searching the left half by setting hi = mid - 1.
Question 19
Question: An algorithm with running time $\Theta(n \log n)$ necessarily takes exactly twice as long when the input size is doubled. - a. True - b. False
Answer: b. False
Explanation:
Let $f(n) = n \log_2 n$. Evaluating $f(2n)$:
$$f(2n) = 2n \log_2(2n) = 2n(\log_2 n + 1) = 2n \log_2 n + 2n = 2f(n) + 2n$$
When the input size is doubled, the running time increases by a factor of $2 + \frac{2}{\log_2 n}$, which is strictly greater than 2.
Question 20
Question: Given the array $[-4, 3, -2, 5, -1, 2, -6, 4]$, what is the maximum contiguous subarray sum? - a. 6 - b. 8 - c. 5 - d. 7
Answer: d. 7
Explanation:
Using Kadane's algorithm, the contiguous subarray $[3, -2, 5, -1, 2]$ yields the maximum sum:
$$3 + (-2) + 5 + (-1) + 2 = \mathbf{7}$$
Question 21
Question: Which statement correctly compares the standard algorithms for the maximum-subarray problem? - a. Brute force and divide-and-conquer both require $\Theta(n)$ time. - b. The standard divide-and-conquer algorithm takes $\Theta(n \log n)$. - c. Kadane's algorithm requires $\Theta(n^2)$ time. - d. Divide-and-conquer always takes $\Theta(n^2)$.
Answer: b. The standard divide-and-conquer algorithm takes $\Theta(n \log n)$.
Explanation:
- Brute Force: Checks all $O(n^2)$ subarrays in $\Theta(n^2)$ time.
- Divide-and-Conquer: Recurrence $T(n) = 2T(n/2) + \Theta(n) \implies \mathbf{\Theta(n \log n)}$.
- Kadane's Algorithm (Dynamic Programming): Single pass in $\Theta(n)$ time.
Question 22
Question: Apply the Master Theorem to $T(n) = 4T(n/2) + n$. Which result is correct? - a. $\Theta(n)$ - b. $\Theta(n \log n)$ - c. $\Theta(n^2)$ - d. $\Theta(n^3)$
Answer: c. $\Theta(n^2)$
Explanation:
For $T(n) = a T(n/b) + f(n)$, we have $a = 4$, $b = 2$, and $f(n) = n$.
1. Compute $n^{\log_b a} = n^{\log_2 4} = n^2$.
2. Compare $f(n) = n$ with $n^{\log_b a} = n^2$: $f(n) = O(n^{2 - \epsilon})$ for $\epsilon = 1$.
3. By Case 1 of the Master Theorem, $T(n) = \mathbf{\Theta(n^2)}$.
Question 23
Question: Which statement is correct for $f(n) = 7n^3 + 4n^2 + 100$? - a. $f(n) = \Theta(n^2)$ - b. $f(n) = \Theta(n^4)$ - c. $f(n) = \Theta(n^3)$ - d. $f(n) = \Theta(2^n)$
Answer: c. $\Theta(n^3)$
Explanation:
In asymptotic notation, lower-order terms ($4n^2 + 100$) and constant coefficients ($7$) are dominated by the highest-degree polynomial term ($n^3$) as $n \to \infty$. Therefore, $f(n) = \mathbf{\Theta(n^3)}$.
Question 24
Question: Which change to the standard merge sort procedure could cause merge sort to lose its stability? - a. Copying the left half before merging - b. Choosing the right-half element first when two current elements have equal keys - c. Using recursion instead of iteration - d. Dividing an array of odd length into unequal halves
Answer: b. Choosing the right-half element first when two current elements have equal keys
Explanation:
Stability demands that elements with equal keys preserve their original relative input order. Standard Merge Sort breaks ties by taking the element from the left subarray first ($L[i] \le R[j]$). Choosing the right-half element first when keys are equal forces the right element ahead of the left element, swapping duplicate keys and destroying stability.
Question 25
Question: Solve the recurrence $T(n) = 2T(n/2) + n$, where $T(1) = \Theta(1)$. - a. $\Theta(n \log n)$ - b. $\Theta(\log n)$ - c. $\Theta(n^2)$ - d. $\Theta(n)$
Answer: a. $\Theta(n \log n)$
Explanation:
Applying the Master Theorem to $T(n) = 2T(n/2) + n$:
1. $a = 2$, $b = 2$, $f(n) = n$.
2. Compute $n^{\log_b a} = n^{\log_2 2} = n^1$.
3. Since $f(n) = \Theta(n^{\log_b a}) = \Theta(n)$, Case 2 of the Master Theorem applies:
$$T(n) = \Theta(n^{\log_b a} \log n) = \mathbf{\Theta(n \log n)}$$