Final Exam Practice Problem Sheet
Final Exam Practice Problem Sheet
The main purpose of this practice problem sheet is to test how well you understand the topics, and
you can expect questions with structures/problem scenarios in the final exam that are different from
the ones in this problem sheet. Also keep in mind that some of the questions in the final exam may be
easier, and some might be equally challenging, in comparison to the average level of difficulty in this
problem sheet.
Furthermore, the set of topics addressed in this problem sheet is not to be taken as the syllabus for
the exam; for the syllabus, please refer to the announcement sent out on May 1.
Finally, this is not a practice exam, but if you really are interested in converting it into one, I would
suggest breaking it into three ≈ 1 hour sessions corresponding to three parts.
1 Greedy Algorithms
1. Given a connected weighted graph G = (V, E, w) where all the weights are non-negative, give an
O(|E| log |V |)-time algorithm to find the minimum weight edge that you can remove from the graph
so that the graph still stays connected.
Example: Let G = (V, E, w) where V = {1, 2, 3, 4, 5}, with edges {1, 2}, {2, 3}, {3, 4}, {4, 1}, {4, 5} having
weights 5, 2, 6, 7, 1 respectively. Then the min wt edge you can remove while keeping the graph connected
is {2, 3}.
2. Give an O(n)-time algorithm for the following problem:
Input. An array a[1 : n] of n real numbers which are sorted in the increasing order.
Output. An interval of length 2 on the real line that contains the maximum number of elements in a.
Example: Suppose a = [1.2, 1.5, 2.9, 3.3, 3.43, 3.45, 3.6]. Then the interval [1.5, 3.5] contains 5 elements
1.5, 2.9, 3.3, 3.43, 3.45 of a.
3. A graph G = (V, E), we want to color the vertices of the graph G such that for every edge {u, v} ∈ E,
the end points u and v are assigned different colors. Give an algorithm for the following problem.
Input. An undirected graph G = (V, E).
Output. A coloring of the vertices of G using at most d + 1 colors, such that the end points of every
edge {u, v} ∈ E are assigned distinct colors. Here d denotes the maximum degree of the graph.
1
2 Divide-and-Conquer
1. Give an O(log n)-time algorithm for the following problem:
Input. A sorted array a[1 : n] of n distinct integers
Output. If there is an index i ∈ [n] satisfying a[i] = i, output Yes, otherwise output No.
Example: Suppose if a = [−1, 0, 3, 7, 9], the a[3] = 3 and hence the output is Yes. For a = [0, 1, 2, 3] the
output is No.
2. Given two sorted arrays a[1 : n] and b[1 : m], give a O(log m) + O(log n) time algorithm to find the k th
smallest element in the union of the two lists.
3. Given two n-digit positive integers a and b, give an O(n2 )-time divide-and-conquer based algorithm for
computing the multiplication a × b. You may count any addition, subtraction, multiplication, division
operation on two single digit integers as 1 unit of operation.
Bonus. Can you give better than quadratic time algorithm for the multiplication problem as well?
3 Dynamic Programming
1. Recall that in class, we saw a dynamic programming based algorithm for solving the Subset-Sum problem,
under the assumption that the input consists of “non-negative” “integers”. In this question, we will deal
with these assumptions:
(a) Does the algorithm taught in class work if the input can consist of negative integers? If not, give
an example of an instance (where some of the integers are negative) for which the algorithm fails to
work. Furthermore, if the algorithm doesn’t work, suggest a modification to the algorithm so that
it can work even when some of input integers are negative.
(b) Does the algorithm work if the input consists of positive real numbers which are not necessarily
integers? If not, explain why.
2. Give an O(n3 log t)-time algorithm for the following problem:
Input. Given a set of positive integers a1 , . . . , aP
n and a positive integer target t.
Output. A subset S of smallest size such that i∈S ai = t.
Example: Suppose the input integers are a1 = 1, a2 = 2, a3 = 3, a4 = 5, a5 = 9 and t = 10. Then the
smallest size subset that sums to 10 is {1, 4} i.e., a1 + a4 = 1 + 9 = 10.
3. Given a string a[1 : n] of n-characters, a subsequence of a is the string formed by taking a subset of
characters. For example a = [c a n b e e a s y], the subsequence corresponding indices 1, 3, 4, 7 is [c n b a].
A string is a palindrome if reads the same forwards and backwards e.g., l e v e l. Give an O(n2 ) time
algorithm for the following problem:
Input. A string a[1 : n] of n-characters.
Output. The subsequence of longest length in a which forms a palindrome.
Example: In a = [f o r m s a p a l i n d r o m e] the longest subsequence which forms a palindrome is
o r a p a r o.
4. Suppose you are given a sequence integers that are separated by + and × signs – e.g.,
1+2×3+4
Adding parenthesis to the expression can change what the expression evaluates to, for example:
(1 + 2) × (3 + 4) = 21
1 + 2 × (3 + 4) = 15
1 + (2 × 3) + 4 = 11.
Page 2
Give an algorithm for the following problem:
Input. A sequence a1 b1 a2 b2 . . . bn−1 an , where a1 , . . . , an are single digit integers and b1 , . . . , bn−1 ∈
{×, +}.
Output. The parenthesization of the expression that yields the maximum value. For instance, in the
above example the algorithm will output (1 + 2) × (3 + 4), which yields 21.
Your algorithm should run in polynomial in n time.
Page 3