CS307 Fall 2006 final Exam suggested solutions and grading criteria.

advertisement
CS307 Fall 2006 final Exam suggested solutions and grading criteria.
1. Answer as written or -2. On Big O questions leaving off O() okay.
On output questions differences in spacing okay.
A.
19
/
12
/
-5
\
21
\
14
B. SITRKNE
C. ITSNKRE
D. TINKERS
E.
F.
G.
H.
I.
J.
K.
L.
No
19 4 13 10 7 16
O(1)
O(N)
100
O(log N)
O(N)
O(N)
M. It is better to sort and then do a binary search.
If the data is not sorted the number of operations is roughly
1.25 x 10^5 * 5 x 10 ^ 5. (Assumes each linear search goes about half way
through the list of items.) This equals 6.25 x 10^10 ops.
If the data is sorted using quicksort, and NlogN sort, and then searched
using
binary search the number of ops is roughly
1 x 10^6 * log
1 x 10^6 * 2 x
2 x 10^7 + 2.5
2 x 10^7 + .25
2.25 x 10^7
1 x 10^6 + 1.25 x 10^5 * log 1 x 10^ 6 =
10^1 + 1.25 x 10^5 * 2 x 10^1 =
x 10^6 =
x 10^7 =
2.25 x 10^7 is much less than 6.25 x 10^10.
Or words and calculations to that effect. (No partial credit.)
N.
n1
n3
----- ----|
| |
|
--|-- --|-|
|
|
|
|
|
V
V
-------------------|
|
|
|
n2
----|
|
--|-|
|
|
V
-------------------|
|
|
|
|
|
/ |
---------->|
|
/ | / |
|
| |
|
|
|
| |
|
|
----|------------------|--------------|
^
|
^
|
|
|
|
------------O. O(N)
2. Suggested Solution:
public boolean isCircular(){
if( head == null )
return false;
Node temp = head;
while( temp.getNext() != null && temp.getNext() != head )
temp = temp.getNext();
return temp.getNext() == head;
}
criteria:
1
3
4
2
point, handle empty case correctly
points, attempt to traverse list
points, correctly traverse list and stop at proper location
points, return correct value
3. Suggest solution:
public int indexOf(int start, Object obj){
// get to start position
Node temp = head;
for(int i = 0; i < start; i++)
temp = temp.getNext();
int position = start;
while( temp != null && !temp.getData().equals( obj ) ){
position++;
temp = temp.getNext();
}
return (temp == null) ? -1 : position;
}
criteria
2 points, attempt to move to location start in list
3 points, correctly move to location start in list
4 points, attempt to locate obj in list or determine not present
4 points, correctly locate obj in list or determine not present
2 points, return correct position of obj or -1 if not present
4. Suggested Solution:
public int numNodesSumOfChildren(){
return helper(root);
}
private int helper(TreeNode n){
// base case
if( n == null || (n.getLeft() == null && n.getRight() == null) )
return 0;
int sumOfChildren = 0;
if( n.getLeft() != null )
sumOfChildren += n.getLeft().getValue();
if( n.getRight() != null )
sumOfChildren += n.getLRight().getValue();
int total = (sumOfChildren == n.getValue() ) ? 1 : 0;
return total + helper(n.getLeft()) + helper(n.getRight() );
}
criteria:
1 points, attempted base case(s)
2 points correct base case(s)
2 points, attempt to check if value in node is equal to children
2 points, correctly check if value in node is equal to children
2 points, attempt to visit child nodes (most likely via recursion)
2 points, correctly visit child nodes (most likely via recursion)
2 points, attempt to return number of nodes that meet criteria
2 points, correctly return number of nodes that meet criteria
5. Suggested solution:
public Map<String, Set<String>> getReverse( Map<String, Set<String>>
originalGuide){
// create the resulting map
Map<String, Set<String>> result = new Map<String, Set<String>>();
// get an iterator for the keys of the original map
Iterator keyIterator = originalGuide.keySet().iterator();
Iterator valIterator;
String value, key;
// iterate through the keys of the original map
while( keyIterator.hasNext() ){
key = keyIterator.next();
// get an iterator for the set associated with this key
valIterator = originalGuide.get( key ).iterator();
// iterator through the strings in the set associated with
the
// current key. If the value is not present in the result as
a
// key add a new set to the result for that value.
while( valIterator.hasNext() ){
value = valIterator.next();
if( !result.containsKey( value ) )
result.put( value, new Set<String>() );
result.get( value ).add( key );
}
}
return result;
}
criteria:
1 point, create result
2 points, attempt to iterate through the keys
2 points, correctly iterate through the keys
2 points, attempt to iterate through the values associated with a key
2 points, correctly iterate through the values associated with a key
3 points correctly create sets for new keys in result
1 point, attempt to add key from original to result
1 point, correctly add key from original to result
1 point, return result
6. Suggested solution:
public void reverseN(Queue q, int n){
Stack rev = new Stack();
Queue normal = new Queue();
for(int i = 0; i < n, i++)
rev.push( q.dequeue() );
while( !q.isEmpty() )
normal.enqueue( q.dequeue() );
while( !rev.isEmpty() )
q.enqueue( rev.pop() );
while( !normal.isEmpty() )
q.enqueue( normal.dequeue() );
}
criteria:
5 points attempt
6 points correct. (Various points off depending on severity of error)
Big O. Must match approach. For the suggested solution assuming all
operations for the Stack are O(1) the suggested solution is O(N) where N
is the number of elements in q. All elements are dequeued in N steps. All
elements are then enqueue in N steps. N + N gives a Big O of (N).
various points off depending on severity of the error.
Download