CompSci 100E, Midterm #2, Nov 15, Fall 2006
Name ____________________ Community Standard Acknowledgment ___________________
Note that this exam has 10 pages: Check!
Watch your time! You only have 75 minutes.
You should not spend too long on any one problem on your first pass.
If you run out of space to do a problem, continue it on the back of the
previous page!
Problem | Value | Points Earned
|
Prob 1 | 6 |
|
Prob 2 | 6 |
|
Prob 3 | 10 |
|
Prob 4 | 10 |
|
Prob 5 | 8 |
|
Prob 6 | 10 |
|
Prob 7 | 10 |
|
Prob 8 | 6 |
|
Prob 9 | 6 |
|
total | 72 |
|
Closed Book
You may use your computer for this exam in the following limited way: You may
refer to the Java 1.5 API for Problems 6 and 7 only. Other problems should
not use external methods or classes other than the String methods.
You should also refer to the separate reference sheet for assumptions
about basic binary tree and linked list nodes.
Page 2
Problem 1: Is everything in order? (6 points)
Below you will find the names of six different sorts that we have
explored in class. For each, rate it by writing words or
phrases preceded by a plus or minus sign to show whether
you consider the rating an advantage or disadvantage of the sort.
Each sort should have at least three rating items and at least one
of these must be positive and at least one must be negative.
Rating items must include speed (big Oh) and could include
memory use, ease of writing
the code, and a certain property of the sort. While all should
have a big Oh rating, it might be a plus for some, a negative for
others. Include other important attributes that would help someone choose
which sort to select for a specific sorting task.
Selection Sort
Insertion Sort
Quicksort
Merge Sort
Radix Sort
Heap Sort
Page 3
Problem 2: A tree in heaps clothing (6 points)
A binary tree can be created and maintained to act like a min heap
with the smallest element at the root. This allows it to be used as a
priority queue. Complete the method isLikeHeap that confirms (or
refutes) that the tree observes the heap property.
(You should ignore the "shape" property
normally associated with heaps.)
/** @param bt is a (possibly empty) binary tree with the info fields containing integers
* @returns true if all info fields of bt exhibit the heap property, false if not */
boolean isLikeHeap(Tree bt) {
Page 4
Problem 3: Oh Oh!? (10 points)
Given the following recurrence relation, derive the associate Big Oh.
You must show your work. The answer alone will yield only partial credit.
T(1) = 1
T(N) = 3 T(N/3) + N
Page 5
Problem 4: Unstable (10 points)
Complete the code for a reverse Selection sort that can sort an array of
Objects that implement Comparable from largest to smallest.
/** @param stuff is a (possibly empty) array of Comparable Objects
* stuff is sorted from largest to smallest */
void reverseSelectionSort(Comparable[] stuff) {
Page 6
Problem 5: Slogging it out (8 points)
Give the recurrence relation for the following pseudo-code. (Do
not
compute the Big Oh)
something(int[] list, int first, int last){
if (first < last) {
process(list, first, last) // an O(n) operation where n is related to last - first
span = last - first;
something(list, first, first + span/4);
something(list, last - span/4, last);
}
}
Page 7
Problem 6: Down for the Count(10 points)
Suppose you want to count and print the number of times each word appears
in a file. The file contains 1,000,000 words, and you expect no more
than 100,000 of them to be distinct. You have room in memory to store
about 300,000 words. Sketch out an efficient approach
to solving this problem, and estimate the time needed. How fast is it?
For this problem, you should assume that the constant multipliers
concealed in the Big-Oh notation for run-times are all 1. (You may use
classes from the Java API, or that you can build. For either type of class,
outline the scheme used in its implementation.)
Page 8
Problem 7: Who's calling? (10 points)
Usually a phone number is looked up by using a person's name as the key,
but sometimes you want the name from the phone number like in caller ID.
Suppose you have a Map of name to number, but want one of number to name.
You also want the constructed map to supply the name given the number in
O(1) time. Complete the below method which will construct
and return a fast reverse lookup map.
/** @param lookup is not null and there are no duplicate values
* @returns a map that reverses the keys and values in lookup. Future
* lookups using the Map returned are O(1).
*/
Map getReverseLookup(Map lookup) {
Page 9
Problem 8: It's only logical ... (6 points)
Assume we have three variables a, b, and c, each one byte long.
Bytes are just like
ints but 8 bits long. For each of the following assume that a and
b start with:
a: 10101010
b: 11001100
Show the contents of c, in binary, after each operation.
- c = a & b;
- c = a | b;
- c = a ^ b;
- c = ~ a;
- c = b >> 4;
- c = b << 4;
Page 10
Problem 9: Follow the chain of command?(6 points)
Assume you have a linked list of nodes containing strings. Process
this list and return the number of times adjacent nodes are out of order
assuming that the list was supposed to be sorted. So if you
encountered the sequence "andy" -> "best" -> ..., that would
be in order and not counted. If you encountered the sequence
"fright" -> "basic", you would note that these are out of order and
would increase your count.
Complete the below method whose header is shown below.
/** @param list: possibly empty, null-terminated, singly-linked list containing Strings
* @returns the number of out-of-order pairs in the list.
*/
int howDisordered(Node list) {