Sie sind auf Seite 1von 12

15-211: Fundamental Data Structures and Algorithms

First Midterm Exam


September 23, 2010
Name:
Andrew ID:
Circle Your Section:
Section A Section B Section C Section D Section E Section F Section G
Jon Aaron Joo-Yeon Ankur Ian Ryan Andrew
9:30-10:20 10:30-11:20 11:30-12:20 12:30-1:20 1:30-2:20 2:30- 3:20 3:30-4:20
Instructions:
Make sure that your exam has 11 pages (not including this cover sheet) and is not missing
any sheets, then write your full name and Andrew login ID on this page (and all the others
if you want to be safe).
Write your answers in the space provided below the problem. If you make a mess, clearly
indicate your nal answer.
The exam has 6 questions, with a maximum score of 73 points.
The problems are of varying diculty. The point value of each problem is indicated. Pile up
the easy points quickly and then come back to the harder problems.
This exam is CLOSED BOOK. You may, however, use a single sheet (8
1
/
2
11) of notes that
you wrote. You may not use a calculator, laptop or any other electronic or wireless device.
Good luck!
Question Points Score
Asymptotics 14
Hashing Recitation C 14
Heaps to the Rescue! 20
SillySort 8
You Splay Me 9
Never Drink And Traverse 8
Total: 73
15-211 First Midterm Exam Page 1 of 11
Question 1: Asymptotics (14 points)
(a) mystery(n) {
for(int i=0; i<n; i++)
mystery(i);
System.out.println(n);
}
(5 points) How many lines (in notation, and in terms of n) does mystery(n) print?
(b) (1 point) Match the notation to its correct denition.
1. T(n) O(f(n))
2. T(n) (f(n))
3. T(n) (f(n))
(a) Notation
1. c
1
> 0c
2
> 0n
0
> 0n > n
0
, c
1
f(n) T(n) c
2
f(n)
2. c > 0n
0
> 0n > n
0
, T(n) cf(n)
3. c > 0n
0
> 0n > n
0
, T(n) cf(n)
(b) Denitions
For each of the following, determine whether f = O(g), or f = (g), or f = (g). You
must choose the tightest bound. No explanation is necessary.
(c) (1 point) f(n) = n 100 and g(n) = n 200
(c)
(d) (1 point) f(n) = n
1/2
and g(n) = n
2/3
(d)
(e) (1 point) f(n) = 100n + log n and g(n) = n + (log n)
2
(e)
(f) (1 point) f(n) = nlog n and g(n) = 10nlog 10n
(f)
(g) (1 point) f(n) = log 2n and g(n) = log 3n
(g)
/ 11
15-211 First Midterm Exam Page 2 of 11
(h) (1 point) f(n) = 10 log n and g(n) = log(n
2
)
(h)
(i) (1 point) f(n) = n
1.01
and g(n) = nlog
2
n
(i)
(j) (1 point) f(n) = n
2
/ log n and g(n) = n(log n)
2
(j)
/ 3
15-211 First Midterm Exam Page 3 of 11
Question 2: Hashing Recitation C (14 points)
Before the classes began, JY wanted to have a hashtable prepared to save and retrieve records
for each student in her recitation by hashing the students with their lastnames.
(a) She came up with the hash function:
hash(lastname) = k
where the lastnames rst character is the k
th
letter in the alphabet.
Thinking that she would have 15 students assigned to her recitation, she concluded that
having a table of size 15 for her recitation would be just right.
To handle collisions, she thought of using quadratic probing.
i. (3 points) What are the two conditions that must be satised in order for quadratic
probing to work?
1.
2.
ii. (3 points) She tried to calculate the probe osets with this table size in order to assess
her initial setup. The following shows the calculation for the probe osets when the
original hash value is 0. Fill in the blanks.
1 mod 15 = 1
4 mod 15 = 4
9 mod 15 = 9
mod 15 =
mod 15 =
mod 15 =
mod 15 =
mod 15 =
mod 15 =
mod 15 =
121 mod 15 =
iii. (2 points) What could be an issue with this setup?
iv. (1 point) How could she resolve the issue?
/ 9
15-211 First Midterm Exam Page 4 of 11
(b) After classes begin, JY nds out that there are 30 students in her recitation instead of 15.
So she increases her table size to 31. Being a hashtable lover, she still chooses to hash her
students by lastname instead of ordering students by some simple algorithm (e.g. ordering
them by their names in alphabetical order.)
Unfortunately, JY nds out that one of her conditions from part (a), section i cannot be
satised if she sets her table size to be as small as 31. So she cannot use quadratic probing.
i. (2 points) Which of the two conditions from part (a), section i is violated? Why?
ii. (3 points) JY can handle collisions in three other ways. For each collision strategy, in
a few words mention why the strategy would work well or would not work well for
the given setup of her recitation. You may assume that her list of students is nal.
1. Linear probing
2. Separate chaining
3. Perfect hashing
/ 5
15-211 First Midterm Exam Page 5 of 11
Question 3: Heaps to the Rescue! (20 points)
Overwhelmed by grading, Ankur recruits you to help him out. He promises that in return, he
will award you 20 points on your midterm exam.
For each of his students, he has an unsorted list of n scores and needs to nd the k smallest
to drop. Unfortunately its taking him too long, because the algorithm hes using takes (kn)
time. He wants you to help him develop an algorithm that takes less time. Note that he hasnt
yet decided a value of k, so your algorithm should work for arbitrary k, n (k is not constant).
(a) (3 points) Complete the pseudocode below describing Ankurs nave algorithm. A HashSet
is a set implemented with a hash table that has only hash keys. The benet of HashSet
is that it supports constant time insertion and membership-test.
/*
* Given a list of scores, L, returns
* a HashSet of the k smallest elements
*/
HashSet ankursAlgorithm(int[] L)
{
lowScores = new HashSet();
for (i in 0,...,_______)
{
m = INT_MAX;
for (j = 0,..., n - 1)
{
if (lowScores doesnt contain ___________)
{
m = min(m, ___________);
}
}
lowScores.add(m);
}
return lowScores;
}
(b) (4 points) You begin to realize that its a good idea to track the smallest k elements of the
array (so far) in a heap, but need to gure out some of the specics. Fill in the following
pseudocode to cement your understanding of a heap-based algorithm. You may assume
the existence of a percolateUp(int[] array, int index) and
percolateDown(int[] array, int index) method.
/*
* Given a list of scores, L, returns
* a heap-array of the k smallest elements
*/
0.) int[] heapAlgorithm(int[] L)
/ 7
15-211 First Midterm Exam Page 6 of 11
1.) {
2.) h = new int[k];
3.) buildHeap(h, L[0...k-1]);
4.) for (i = k...n-1)
5.) {
6.) if (L[k] ______ h[0])
7.) {
8.) h[______] = L[k]
9.) __________________(h, ___);
10.) }
11.) }
12.) return h;
13.) }
(c) (2 points) (c)
Is h a min-heap or a max-heap?
(d) (2 points) In terms of k, n what is worst-case runtime (big ) of lines 4-11?
(e) Assume buildHeap() is written naively (calls insert() on every element)
i. (2 points) What is the runtime of line 3 (big )?
ii. (2 points) What is the runtime of the entire algorithm (big )?
(f) Assume buildHeap() is written intelligently (iterates with percolateDown())
i. (2 points) What is the runtime of line 3 (big )?
ii. (2 points) What is the runtime of the entire algorithm (big )?
iii. (1 point) Assuming n is much bigger than k (n k), what is the runtime of the
entire algorithm (big )?
/ 13
15-211 First Midterm Exam Page 7 of 11
Question 4: SillySort (8 points)
Consider the following recursive sorting algorithm, called SillySort. SillySort takes as input an
array, A. Let i and j be the left and right indecies into the array, respectively.
If A[j] < A[i], then swap A[i] and A[j].
If j > i, then
1. SillySort the rst 2/3 of the array
2. SillySort the nal 2/3 of the array
3. SillySort the rst 2/3 of the array (again)
Assume that this algorithm is correct. We will determine the run-time complexity of the
algorithm using the Master Method.
(a) (1 point) Let a be the number of sub-problems solved recursively. What is a?
(a)
(b) (2 points) Each sub-problem is of size n/b. What is b?
(b)
(c) (1 point) Let f be the non-recursive overhead. What is f, asymptotically?
(c)
(d) (1 point) Write the recurrence relation for the algorithm. You may omit the base-case.
(d)
(e) (2 points) Use the Master Method to solve the recurrence. Express the answer in terms
of the size of the input, n, and your answers for a and b (i.e., you do not have to simplify
the expression.)
(e)
(f) (1 point) Is SillySort asympotically better, worse, or the same as MergeSort?
(f)
/ 8
15-211 First Midterm Exam Page 8 of 11
Question 5: You Splay Me (9 points)
Determine if the following trees are valid splay trees. You may assume that only insert oper-
ations have been performed (i.e. there were no calls to nd or remove). If the tree is a valid
splay tree, list a possible order in which the elements could have been inserted in order to form
the tree. If the tree is not a valid splay tree, briey explain why.
(a) (3 points) Tree A:
3
1
2 4
5
(b) (3 points) Tree B:
1
2
3
4
5
6
7
/ 6
15-211 First Midterm Exam Page 9 of 11
(c) (3 points) Tree C:
1
2
3
4
5
/ 3
15-211 First Midterm Exam Page 10 of 11
Question 6: Never Drink And Traverse (8 points)
A student (who shall remain nameless) went out one night and had a few too many. The
student then (in what has been considered one of the nerdiest drunken decisions ever) decided
to write his own tree traversal algorithm. Here is the pseudocode for that algorithm:
function drunkTraverse(Node current)
{
if current.left doesnt exist {
print "-1"
return
}
if current.right exists {
print current.right
drunkTraverse(current.left)
print current.left
drunkTraverse(current.right)
}
}
(a) (3 points) Perform the drunkTraverse algorithm on the root of the following tree:
1
2
3 4
5
6
(a)
The next morning the student awoke with a feeling that he had discovered the most
beautiful tree he had ever seen before. Unfortunately, he couldnt remember what it
looked like. He found a note that he had written from the night before that described the
tree.
The in-order traversal of the tree is 0, 2, 7, 1, 3, 5, 6.
/ 3
15-211 First Midterm Exam Page 11 of 11
The drunkTraversal of the tree is 5, 1, -1, 0, 2, -1.
Recall that an in-order traversal rst traverses the left subtree, then prints out the current
node, and then traverses the right subtree.
(b) (5 points) From these two facts, either reconstruct the tree, or explain why it isnt possible
to reconstruct the tree.
/ 5

Das könnte Ihnen auch gefallen