Mid s12
Mid s12
Midterm
This test has 9 questions worth a total of 60 points. You have 80 minutes. The exam is closed
book, except that you are allowed to use a one page cheatsheet. No calculators or other electronic
devices are permitted. Give your answers and show your work in the space provided. Write out
and sign the Honor Code pledge before turning in the test.
“I pledge my honor that I have not violated the Honor Code during this examination.”
1
2 PRINCETON UNIVERSITY
0. Miscellaneous. (1 point)
In the space provided on the front of the exam, write your name and Princeton NetID; circle
your precept number; and write out and sign the honor code.
int min = N;
for (int i = 0; i < N; i++) {
Selection.sort(a, a[i].BY_POLAR_ORDER);
for (int j = 0; j < N; j++) {
for (int k = j+1; k < N; k++) {
if (a[j].distanceTo(a[k]) <= 1.0) {
min = Math.min(min, k - j);
}
}
}
}
(a) Suppose that the code fragment takes 30 seconds when N = 2, 000. Estimate the running
time (in seconds) as a function of the input size N . Use tilde notation to simply your
answer.
(b) Suppose that you replace the call to Selection.sort() with a call to Merge.sort().
What is the order of growth of the running time of the modified 11-line code fragment
as a function of N ? Circle the best answer.
1 N N log N N2 N 2 log N N3 2N
COS 226 MIDTERM, SPRING 2012 3
(a) Match up each quantity on the left with the best matching quantity on the right. You
may use a letter more than once or not at all.
1
−−− Max height of a binary heap with N keys. B. ∼ 2 lg N
(b) A sorting algorithm is parsimonious if no pair of items is compared more than once.
Circle the following sorting algorithms (as implemented in lecture and the textbook) if
they are parsimonious; cross them out if they are not parsimonious.
(a) Using the 64-bit memory cost model from the textbook, how much memory (in bytes)
does a StringBuilder object use to store a sequence of N characters? Simplify your
answer using tilde notation.
• Best case:
• Worst case:
(b) What is the order of growth of the amortized running time of each of operation below?
Write down the best answer in the space provided, using one of the following functions.
√
1 log N N N N log N N2
lynx bass lion bass bass bass bass gnat wren bass
bass bear frog bear bear bear bear bass worm bear
bear crab mole clam crab clam clam bear oryx clam
crab lion hawk crab lynx crab crab crab swan crab
lion goat wren frog frog frog crow lion wolf crow
goat duck lynx gnat goat goat deer goat mule deer
mole frog crab goat lion hawk dove duck mole dove
frog dove swan hawk mole lion duck frog puma duck
swan clam bear lion clam lynx frog dove seal frog
clam hawk clam lynx hawk mole gnat clam deer gnat
hawk deer bass lynx swan swan goat hawk lion goat
wren crow goat mole wren wren hawk deer goat hawk
mule gnat mule mule gnat gnat mule crow bear lion
oryx lynx oryx oryx lynx lynx oryx lynx lynx lynx
gnat lynx gnat swan mule mule lynx lynx gnat lynx
lynx puma lynx wren oryx oryx lynx oryx lynx mole
puma worm puma puma crow puma puma puma frog mule
worm seal worm worm puma worm worm worm crab oryx
seal oryx seal seal seal crow seal seal bass puma
crow mule crow crow worm deer lion mule crow seal
deer wolf deer deer deer dove wren wren clam swan
wolf wren wolf wolf dove duck wolf wolf hawk wolf
dove swan dove dove duck seal mole swan dove worm
duck mole duck duck wolf wolf swan mole duck wren
---- ---- ---- ---- ---- ---- ---- ---- ---- ----
0 1
red link
O
F ?
B L S Z
A D J M Q T Y
C I K P
(a) Which one or more of the keys below could be the one labeled with a question mark?
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
(b) Which one or more of the keys below must be red (link between it and its parent is red)?
P Q S T Y
(c) How many left rotation, right rotation, and color flip operations would be used to insert
each key below into the original red-black BST above?
E N G
rotateLeft()
rotateRight()
flipColors()
COS 226 MIDTERM, SPRING 2012 7
6. Hashing. (6 points)
Suppose that the following keys are inserted in some order into an initially empty linear-
probing hash table of size 7 (assuming no resizing), using the following table of hash values:
key hash
A 5
B 2
C 5
D 1
E 4
F 1
G 3
(a) Give the contents of the linear-probing array if the keys are inserted in alphabetical
order: A, B, C, D, E, F, G.
0 1 2 3 4 5 6
(b) Which of the following could be the contents of the linear-probing array if the keys are
inserted in some other order?
0 1 2 3 4 5 6
I.
A F D B G E C
0 1 2 3 4 5 6
II.
F A D B G E C
0 1 2 3 4 5 6
III.
C A B G F E D
Partial credit for N log N or for using a linear amount of extra memory.
(a) Give a crisp and concise English description of your algorithm in the box below. Your
answer will be graded on correctness, efficiency, clarity, and conciseness.
(b) What is the order of growth of the worst case running time of your algorithm?
Circle the best answer.
(c) How much extra memory does your algorithm use? Circle the best answer.
1 log M log N M N MN N2
COS 226 MIDTERM, SPRING 2012 9
You should implement the sample() method in constant time and the delRandom() method
in time proportional to log N , where N is the number of keys in the data structure. For
simplicity, do not worry about resizing the underlying array.
• sample(): 85
• delRandom():