1
Practice Midterm 2
1. The naive recursive Fibonacci algorithm runs in exponential time because:
(a) It divides problems unevenly.
(b) It lacks optimal substructure.
(c) It uses recursion instead of iteration.
(d) It recomputes the same subproblems.
2. Consider the following recursive algorithm.
1: procedure M(n)
if n ≤ 1 then
return 1
4:
end if
5:
return M (n − 1) + M (n − 1)
6: end procedure
2:
3:
What is the most accurate running time of the algorithm as written before and after adding
memoization?
(a) O(n) before, O(n) after.
(b) O(2n ) before, O(n) after.
(c) O(2n ) before, O(2n ) after.
(d) O(n2 ) before, O(n) after.
3. You are given an n×m grid where each square contains a positive integer representing the cost
of visiting that square. Starting at the top-left square, you want to reach the bottom-right
square while minimizing the total cost. You may only move right or down.
Consider the following two algorithms (Assume that out-of-bounds array accesses are handled
correctly).
1: procedure Algorithm1(i, j)
if i = n and j = m then
return cost[i][j]
4:
end if
5:
return cost[i][j] + min(
6:
M inCost1(i + 1, j),
7:
M inCost1(i, j + 1))
8: end procedure
2:
3:
1
1: procedure Algorithm2(i, j)
if i = n and j = m then
return cost[i][j]
4:
end if
5:
if cost[i + 1][j] < cost[i][j + 1] then
6:
return cost[i][j] + M inCost2(i + 1, j)
7:
else
8:
return cost[i][j] + M inCost2(i, j + 1)
9:
end if
10: end procedure
2:
3:
Which statement is most accurate?
(a) Algorithm 1 is correct and Algorithm 2 is not.
(b) Algorithm 2 is correct and Algorithm 1 is not.
(c) Both algorithms are correct, but Algorithm 2 is more efficient.
(d) Neither algorithm is correct.
4. Consider the following slightly different variant of edit distance. You are given two strings
A[1 . . . m] and B[1 . . . n]. Let E[i][j] denote the minimum edit distance between the prefixes
A[1 . . . i] and B[1 . . . j], where the cost of an insertion or deletion is 1, and the cost of a
substitution is 2. Assume that E[0][j] = j and E[i][0] = i. Which of the following recurrences
correctly defines E[i][j] for 1 ≤ i ≤ m and 1 ≤ j ≤ n? Note: [A[i] ̸= B[j]] is 1 if A[i] and B[j]
are different and 0 if they are the same.
(a) E[i][j] = min E[i − 1][j] + 1, E[i][j − 1] + 1, E[i − 1][j − 1] + [A[i] ̸= B[j]]
(b) E[i][j] = min E[i − 1][j] + 2, E[i][j − 1] + 2, E[i − 1][j − 1] + [A[i] ̸= B[j]]
(c) E[i][j] = min E[i − 1][j] + 1, E[i][j − 1] + 1, E[i − 1][j − 1] + 2
(d) E[i][j] = min E[i − 1][j] + 1, E[i][j − 1] + 1, E[i − 1][j − 1] + 2 [A[i] ̸= B[j]]
5. You are given n intervals, where each interval has a starting time and an ending time. You
want to select the maximum number of non-overlapping intervals. Two intervals are considered non-overlapping if one ends before or when the other begins.
Consider the following two approaches:
Algorithm 1 (Greedy):
(a) Sort the intervals by increasing ending time.
(b) Select the interval that ends earliest.
(c) Continue selecting the next interval that does not overlap with the previously selected
interval.
Algorithm 2 (Dynamic Programming):
First, sort the intervals by increasing ending time. Let OP T (j) denote the maximum number
of non-overlapping intervals that can be selected from the first j intervals.
The recurrence is:
OP T (j) = max(OP T (j − 1), 1 + OP T (p(j)))
2
where p(j) is the largest index less than j such that interval p(j) does not overlap with interval
j. Assume p(j) is computed by scanning the previous intervals.
Which statement is correct?
(a) Algorithm 1 is incorrect, Algorithm 2 is correct.
(b) Algorithm 2 is incorrect, Algorithm 1 is correct.
(c) Both algorithms are correct and run in O(n log n) time.
(d) Both algorithms are correct, Algorithm 1 runs in O(n log n) time, and Algorithm 2 runs
in O(n2 ) time.
6. Which of the following statements about Huffman coding is correct?
(a) It generates prefix-free codes.
(b) It uses a dynamic programming approach.
(c) It only works for equal frequency symbols.
(d) It guarantees fixed-length codes.
7. Which of the following set of codes for four characters might be generated by Huffman algorithms
(a) A : 01, B : 00, C : 10, D : 11.
(b) A : 01, B : 100, C : 101, D : 11.
(c) A : 01, B : 010, C : 101, D : 11.
(d) A : 0, B : 1, C : 11, D : 10.
3
2
Answers
1. d
2. b
3. a
4. d
5. d
6. a
7. a
4