Final Solutions
Final Solutions
Final Solutions
Final
• Do not open this quiz booklet until directed to do so. Read all the instructions on this page.
• When the quiz begins, write your name on the top of every page of this quiz booklet.
• You have 180 minutes to earn a maximum of 180 points. Do not spend too much time on
any one problem. Read through all problems first, then solve them in an order that allows
you to make the most progress.
• You are allowed three double-sided letter-sized sheet with your own notes. No calcula-
tors, cell phones, or other programmable or communication devices are permitted.
• Write your solutions in the space provided. Pages will be scanned and separated for grading.
If you need more space, write “Continued on S1” (or S2, S3, S4, S5, S6) and continue your
solution on the referenced scratch page at the end of the exam.
• Do not waste time and paper rederiving facts that we have studied in lecture, recitation, or
problem sets. Simply cite them.
• When writing an algorithm, a clear description in English will suffice. Pseudo-code is not
required. Be sure to briefly argue that your algorithm is correct, and analyze the asymp-
totic running time of your algorithm. Even if your algorithm does not meet a requested
bound, you may receive partial credit for inefficient solutions that are correct.
Name:
School Email:
2 6.006 Final Name
(a) [1 point] Write your name and email address on the cover page.
Solution: OK!
(a) T F If f (c) = O(1) for a constant c and f (n) = 4f (n/2) + Θ(n2 ) for n > c,
and g(c) = O(1) for a constant c and g(n) = 8g(n/2) + Θ(n) for n > c,
then f = Ω(g).
Solution: False. f is Θ(n2 log n) and g is Θ(n3 ), by Master theorem.
So f ∈ o(g) and f ∈
/ Ω(g), since Ω(g) ∩ o(g) = ∅.
(b) T F Given an array of n integers representing a binary min-heap, one can find and
extract the maximum integer in the array in O(log n) time.
Solution: False. The maximum element could be in any leaf of the heap, and
a binary heap on n nodes contains at least Ω(n) leaves.
(c) T F Any binary search tree on n nodes can be transformed into an AVL tree using
O(log n) rotations.
Solution: False. Since any rotation changes height of any node by at most a
constant, a chain of n nodes would require at least Ω(n − log n) rotations.
4 6.006 Final Name
(d) T F Given an array of n key-value pairs, one can construct a hash table mapping keys
to values in expected O(n) time by inserting normally into the hash table one at
a time, where collisions are resolved via chaining. If the pairs have unique keys,
it is possible to construct the same hash table in worst-case O(n) time.
Solution: True. Because keys are unique, we do not have to check for duplicate
keys in chain during insertion, so each insert can take worst-case O(1) time.
(e) T F You implement a hash table using some specific hash function h(k), and then
insert a sequence of n integers into the hash table. Then looking up whether a
queried integer is stored in this hash table will take expected O(1) time.
Solution: False. Expected bounds are only valid when choosing a hash func-
tion randomly from a universal hash family.
(f) T F Given a graph where all edge weights are strictly greater than −3, a shortest path
between vertices s and t can be found by adding 3 to the weight of each edge and
running Dijkstra’s algorithm from s.
Solution: False. Counter example: a graph on vertices s, t, v, with undirected
weighted edges w(s, v) = 0, w(s, t) = −1, and w(v, t) = −2.
6.006 Final Name 5
(g) T F Given a directed acyclic graph having positive edge weights and only one source
vertex s having no incoming edges, running Dijkstra’s from s will remove ver-
tices from the priority queue in a topological sort order.
Solution: False. Counter example: a graph on vertices s, t, v, with directed
weighted edges w(s, t) = 1, w(s, v) = 2, and w(v, t) = 1. Dijkstra from s
processes vertices in order (s, t, v), while topological sort order is (s, v, t).
(h) T F Given an unweighted directed graph having only one source vertex s having no
incoming edges, a depth-first search from s will always generate a DFS tree con-
taining a longest simple path in the graph.
Solution: False. For a longest simple path to exist in the DFS tree, DFS would
need to search edges in the longest path before others, while a valid DFS may
chose to first search any one of its unvisited neighbors.
(b) [3 points] Fix the bug in the code by changing a single line.
Solution: Change A[r] > A[l] in line 10 to A[r] < A[l]. In fact there is another
bug in the code: A.pop() should occur prior to the while loop, storing the maximum
value to return later. Students received full points for mentioning either bug.
Rubric:
• 3 points for a correct line change
(c) [4 points] Give an array A of four integers satisfying the max-heap property for which
the original code does not behave as desired.
Solution: A = [3, 1, 2, 0] would yield array [1, 0, 2], which does not satisfy the max-
heap property. Any max-heap on four distinct values would suffice.
Rubric:
• 4 points for a valid max-heap on four distinct values
6.006 Final Name 7
(a) [4 points] You want to store your books on a single shelf sorted by color. The color
of each book is specified by tuple of red, green, and blue values, each in the range 1
to 16. Place your books on the shelf sorted first by red, then green, then blue value.
Solution: Radix sort the books by color, using counting sort to sort first by blue
value, placing the books into at most 16 stacks, then sort by green, then red value.
This is very efficient as each book is moved four times.
(b) [4 points] You are sick of the colorful ordering of your books, and instead want to
sort your books by height. The only way to compare the height of two books is to
take them off the shelf and compare them. You would like to sort your books by only
removing two books from the shelf at a time, then putting them back in the same or
swapped order. Sort your books by height using swaps.
Solution: Use heap sort to sort the books using at most O(n log n) swaps. We choose
heap sort because it is an optimal comparison sort algorithm that is also in place.
(c) [4 points] You’ve bought three new books and want to add them to your bookshelf
currently sorted by height, but you only want to move any book currently on the shelf
at most once. Place the new books on the shelf, assuming you may take a book off the
shelf, compare it with a new book, and place it back on the shelf wherever you like.
Solution: Insertion sort the three books down from the right to their respective places,
by removing each book, comparing to the largest unplaced new book, and placing it
back on the shelf to right of its previous location by the number of new books having
smaller height. This algorithm requires at most n moves and at most n comparisons.
Rubric:
• 1 point for a correct algorithm
• 2 points for algorithm that is well suited to problem
• 1 point for justification
8 6.006 Final Name
Describe a data structure that supports all four 2D priority queue operations, each in worst-case
O(log n) time, where n is the number of pairs in the queue at the time of the operation. You may
assume that each pair stored in the queue is unique.
Solution: Here are two possible solutions using AVL trees. All AVL operations are worst-case.
1) Store each pair in two AVL trees, tree X sorted lexicographically by x then y, and tree Y
sorted lexicographically by y then x. For insert(x,y), insert into each AVL tree in O(log n)
time. For extract(x,y), find and remove the pair in each AVL tree, also in O(log n) time.
For extract min x(), find the minimum pair (x0 , y 0 ) from pairs in AVL tree X, where x0 is
minimum, and extract from X in O(log n) time. Then find (x0 , y 0 ) in AVL tree Y and extract from
Y in O(log n) time. The procedure for extract min y() is symmetric.
2) Store pairs in a single AVL tree sorted lexicographically by x then y. Support insert(x,y)
and extract(x,y) directly using AVL insert and delete. Augment each node n with the
minimum y for any node in n’s subtree, which can be maintained during dynamic operations in
O(log n) time. Specifically, n.ymin = min(n.key.y, n.lef t.ymin, n.right.ymin). To extract a
pair with minimum x, extract and return the node in the AVL with minimum key in O(log n)
time, as finding minimum and deletion on an AVL tree can both be done in O(log n) time. To
extract a pair with minimum y in the subtree rooted at node n, compare n.key.y, n.lef t.ymin,
and n.right.ymin. If n.key.y is minimum, extract and return n. Otherwise, if n.lef t is minimum,
recursively extract minimum in n.lef t’s subtree, and similarly for n.right. This procedure walks
down the tree of height O(log n) and makes a single extraction, so can be done in O(log n) time.
Rubric:
• 2 points for using an AVL or other balanced BST
• 2 points each for correct insert(x,y) and extract(x,y)
• 3 points each for correct extract min x() and extract min y()
• 1 point for correct running time analysis for each operation
• Partial credit may be awarded
6.006 Final Name 9
You can use this paper to write a longer solution if you run out of space, but be sure to refer to
the scratch paper next to the question itself.
SCRATCH PAPER 2. DO NOT REMOVE FROM THE EXAM.
You can use this paper to write a longer solution if you run out of space, but be sure to refer to
the scratch paper next to the question itself.
SCRATCH PAPER 3. DO NOT REMOVE FROM THE EXAM.
You can use this paper to write a longer solution if you run out of space, but be sure to refer to
the scratch paper next to the question itself.
SCRATCH PAPER 4. DO NOT REMOVE FROM THE EXAM.
You can use this paper to write a longer solution if you run out of space, but be sure to refer to
the scratch paper next to the question itself.
SCRATCH PAPER 5. DO NOT REMOVE FROM THE EXAM.
You can use this paper to write a longer solution if you run out of space, but be sure to refer to
the scratch paper next to the question itself.
SCRATCH PAPER 6. DO NOT REMOVE FROM THE EXAM.
You can use this paper to write a longer solution if you run out of space, but be sure to refer to
the scratch paper next to the question itself.