MIT6 006S20 q1 Sol
MIT6 006S20 q1 Sol
Solution: Quiz 1
• 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 120 minutes to earn a maximum of 120 points. Do not spend too much time on
any one problem. Skim them all first, and attack them in the order that allows you to make
the most progress.
• You are allowed one double-sided letter-sized sheet with your own notes. No calculators,
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) and continue your solution on
the referenced scratch page at the end of the exam.
• Do not spend time and paper rederiving facts that we have presented in lecture or recitation.
Simply cite them.
• When writing an algorithm, a clear description in English will suffice. Pseudo-code is not
required. Be sure to argue that your algorithm is correct, and analyze the asymptotic
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.
• Pay close attention to the instructions for each problem. Depending on the problem,
partial credit may be awarded for incomplete answers.
Name:
(a) [1 point] Write your name and email address on the cover page.
Solution: OK!
i. Worst-case: i. Worst-case:
Solution: O(n2 ) Solution: O(n + k)
Common Mistakes: Saying doing a O(1)-time operation m times takes amortized O(m) time.
4 6.006 Solution: Quiz 1 Name
93
85 38
23 63 11 13
17
(a) [6 points] Suppose the tree drawn above is the implicit tree of a binary max-heap H.
State the array representation of H, first before and then after performing the opera-
tion H.delete max().
Solution:
Before: [93, 85, 38, 23, 63, 11, 13, 17]
After: [85, 63, 38, 23, 17, 11, 13]
Common Mistakes:
• Not building heap correctly (e.g., confusing heap order with traversal order)
• Not correctly deleting min (by swapping root with last leaf and heapifying down)
(b) [4 points] Suppose instead that the original tree drawn above is a Sequence AVL
Tree S (note Sequence data structures are zero-indexed). The items in the leaves of
S in traversal order are (17, 63, 11, 13). Perform operation S.delete at(3) on S
including any rotations, and then list the items stored in the leaves of S in traversal
order, after the operation has completed. (You do not need to draw the tree.)
Solution: (17, 85, 11, 13)
Common Mistakes:
• Indexing by heap order instead of traversal order (or using 1-indexing)
• Listing full traversal order (rather than just the leaves)
• Deleting the item but not rebalancing the tree to satisfy the AVL Property
6 6.006 Solution: Quiz 1 Name
(a) [5 points] For House Puffle Huff, students must be sorted by friend number, i.e.,
how many of the other n − 1 incoming students they are friends with, which can be
determined in O(1) time.
Solution: Friend numbers are non-negative integers less than n, so we can use count-
ing sort to sort the students in worst-case O(n) time. (Radix sort also works with the
same running time.) Since we have to compute friend number for each student, any
algorithm will take at least Ω(n) time, so this is optimal.
Common Mistakes: Using a sort that is not O(n)
(b) [5 points] For House Craven Law, students must be sorted by the weight of their
books. Book weights cannot be measured precisely, but the Sorting Sock has a scale
that can determine in O(1) time whether one set of books has total weight greater than,
less than, or equal to another set of books.
Solution: A scale weighing is a comparison with a constant number of outcomes, so
the comparison sort Ω(n log n) lower bound applies. So we cannot do better than by
using an worst-case O(n log n) sorting algorithm, e.g., merge sort, using the scale to
compare one student’s books against another’s.
Common Mistakes: Insufficient justification for why O(n log n) is optimal
6.006 Solution: Quiz 1 Name 7
(c) [5 points] For House Driven Gore, students must be sorted by bravery, which can’t
be directly measured or quantified, but for any set of students, the Sorting Sock can
determine the bravest among them in O(1) time, e.g., by presenting the students with
a scary situation.
Solution: We can’t quantify bravery, so we can’t hope to use any integer-based
algorithms. However, the Sorting Sock can find a student of maximum bravery in
O(1) time, so we repeatedly find and select a bravest student among all previously
unselected students in worst-case O(n) time, which is again optimal. (This is priority
queue sort, using the Sorting Sock as the priority queue to find the maximum.)
Common Mistakes: Arguing a Ω(n log n) lower bound
(d) [5 points] For House Leather Skin, students must be sorted by their magical lineage:
how many of a student’s ancestors within the previous 3dlog ne + 4 generations were
magical. Recall that humans, magical or not, always have two parents in the previous
generation, unlike binary tree nodes which have at most one. Assume the Sorting Sock
can compute the magical lineage of a student in O(1) time.
Solution: Each student has at most 2k ancestors in the kth generation preceding.
Thus
Pthe number of wizard ancestors will be a non-negative number bounded above
3dlog ne+4 k
by i=1 2 < 23(log n+1)+5 = 28 23 log n = 28 n3 log 2 = O(nc ) for any c > 3 log 2.
Thus we can use radix sort to sort the students by their magical lineage in worst-case
O(n) time, which is again optimal.
Common Mistakes:
• Claiming 3dlog ne + 4 ancestors instead of O(23dlog ne+4 )
• Saying that 23dlog ne+4 = O(n) and using counting sort
8 6.006 Solution: Quiz 1 Name
• Using counting/radix sort or creating a direct access array (no bound on u so not efficient)
• Saying n insertions into a hash table gives an amortized bound
• Checking all triples in Ω(n3 ) time
6.006 Solution: Quiz 1 Name 9
• Case 1, u is the left child of u.parent: then all the nodes preceding v in the subtree of
u.parent are in the subtree of u, so set #v (u.parent) = #v (u).
• Case 2, u is the right child of u.parent: then all nodes in the left subtree of u.parent
precede v (as does u), so set #v (u.parent) = 1 + u.parent.left.size + #v (u).
Then return #v (r), since this is the number of nodes preceding v in r’s subtree (i.e., the entire
tree). Correctness is argued within the algorithm description. This algorithm spends worst-case
O(1) work for each ancestor of v, so since the Sequence AVL Tree is balanced, the number of
ancestors is bounded by O(log n), and the algorithm runs in worst-case O(log n) time.
Common Mistakes:
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S1” on the problem statement’s page.
Solution: (Problem 6 continued...)
Worst-case Solution
Sort A and B increasing using merge sort. We give a two-finger algorithm to determine whether
any a ∈ A and b ∈ B sum to a given −c. Doing this for each c ∈ C directly determines whether
a, b, c sum to 0.
Start with an index i = 0 into A and an index j = n − 1 into B and repeat the following procedure
until either i = n or j = −1:
So the claim is true. Then if the algorithm terminates without returning Yes, either i = n or
j = −1, so the claim implies that no integer from either A or B respectively can be in a triple with
c that sums to zero.
Running Time: Sorting A and B takes worst-case O(n log n) time. We perform the two-finger
algorithm n times. A single two-finger algorithm takes worst-case O(n): every loop does O(1)
work and either increases i or decreases j, so since the loop terminates when either i = n or
j = −1, the loop executes at most 2n = O(n) times. So the algorithm runs in worst-case O(n2 )
time in total.
6.006 Solution: Quiz 1 Name 13
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S2” on the problem statement’s page.
Solution: (Problem 8 continued...)
To implement initialize(H), initialize empty D and C, and then for each (p, d) ∈ H, construct
an empty Set AVL Tree Tp , insert d into Tp , insert p into D mapping to Tp . Then build a hash table
D∞ on every (p, d) ∈ H, store it in Q with key ∞, and then insert each (p, d) into C mapping to
D∞ . Building D∞ takes expected O(n) time, and for each (p, d), this procedure takes O(1) time
(expected in the case of inserting into D). So it takes expected O(n) time in total, and maintains
the invariants of the database directly. (Note that we could not use a Set AVL Tree for D, as it
could take Ω(n log n) time to construct.)
To implement report(p, d), lookup p in D to find Tp , and then insert d into Tp .
• If d has no predecessor or successor in Tp , then insert (p, d) into Q with key ∞ and insert
(p, d) into C mapping to its location in Q.
• Otherwise d has a predecessor or successor in Tp .
– If d has a predecessor d1 and successor d2 , lookup (p, d1 , d2 ) in C to find it in Q, and
then remove (p, d1 , d2 ) from both C and Q.
– Otherwise, it has one of them d0 , so lookup (p, d0 ) in C to find it in Q and then remove
(p, d0 ) from both C and Q.
– In either case, if d has a predecessor d1 , add (p, d1 , d) to Q with key |d − d1 | and add
(p, d1 , d) to C pointing to its location in Q;
– and if d has a successor d2 , add (p, d, d2 ) to Q with key |d2 − d| and add (p, d, d2 ) to C
pointing to its location in Q.
This procedure does a constant number of worst-case O(log k) time or amortized expected O(1)
time operations, so this operation runs in amortized expected O(log k) time, and maintains the
invariants of the database by removing any consecutive pairs or lonely holes if they are no longer
consecutive or lonely, and adding any new consecutive pairs or lonely holes that may have been
introduced.
To implement patch(), delete the minimum item from Q containing one or two holes on pipe
p, remove it from C, lookup p in D to find Tp , and remove the relevant holes from Tp . This
procedure does a constant number of worst-case O(log k) time or amortized expected O(1) time
operations, so this operation runs in amortized expected O(log k) time, and maintains the invariants
of the database directly. It is correct because Q exactly implements the requested priority scheme:
consecutive pairs with smaller key have higher priority, and will only remove a lonely hole if there
are no consecutive pairs having finite key contained in Q.
Common Mistakes: See scratch page S3...
14 6.006 Solution: Quiz 1 Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S3” on the problem statement’s page.
Common Mistakes: (For Problem 8)
• Taking a minimum in some pipe rather than a minimum over all pipes
• Not prioritizing pipes having more than one hole over those with only one
• Storing an AVL or heap on all pipes which cannot be maintained within the time bounds
• Attempting an augmentation-based solution incorrectly
• Claiming O(log n)-time insertion into a sorted array
• Initializing a direct access array of non-polynomially bounded size
• Using Sequence AVL Trees instead of Set AVL Trees
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms