CS 307 Fall 2003 Midterm 2 Suggested Solutions and Grading Criteria 1. Answer as written or -3. Partial credit only where noted. On Big O problems no points off for missing O( ) A. O(N) B. O(N) C. O(N^3) D. O(N^2) E. 18 F. 6 G. O(logN) base 2 okay, but not necessary H. 500 sec I. 8 * 23 * 2 / 20 or 92 / 5 or 18.4 +/- 1 seconds J. The worst case can occur if the pivot always picks the lowest or highest value in the list (Or words to that effect.) 2. return helper(a, b, distance, 0, new ArrayList()); private boolean helper(City cur, City dest, int limit, int soFar, ArrayList visited) { boolean result = false; if(cur.equals(dest)) result = soFar <= limit; else if(beenTo.contains(cur) || soFar > limit) result = false; else { //haven't been here on current route. soFar.add(cur); ArrayList connections = cur.connectsTo(); City next; for(int i = 0; i < connections.size() && !result; i++) { next = (City)(connections.get(i)); if( helper( next, dest, limit, soFar + cur.distanceTo(next), visited) result = true; } // remove from list for possible alternate routes visited.remove( visitied.size() - 1 ); } return result; } -12 failure to do exhaustive search -5 possible infinite recursive loop / Already Been Here? ABH? -2 Minor logic errors (LE) -4 - -7, Medium Logic Errors -8 - -15 Major Logic Errors, Gross Conceptual Errors, GCEs -3 early return / problems with return 3. Set result = new Set(); for(int i = 0; i < myCon.size(); i++) { if(otherSet.myCon.contains(myCon.get(i)) result.myCon.add(myCon.get(i)); } return result; -6 always confuses Set var with ArrayList var. Example otherSet.get(i); -2 1 or 2 mistakes in confusion Set var with ArrayList var but otherwise demonstrates knowledge on the difference between a Set and its internal Storage container -4 assuming and calling some method from Set class other than default constructor -2 minor logic errors (off by one) -4 medium logic errors (not checking all data) -8 major logic errors (don't create a new Set, change old Sets) -0 for syntax errors when intent is clear 4. Solution 1 iterative if(iMySize > 1) { myTail = myHead; Node trailer = myHead; myHead = myHead.getNext(); trailer.setNext(null); Node scout = myHead; for(int i = 1; i < iMySize i++) { scout = scout.getMext(); myHead.setNext(trailer); trailer = myHead; myHead = scout; } myHead = temp; } iterative with native array of Objects, moves data instead of altering list structure if(iMySize > 1) { Object[] data = new Object[iMySize]; Node temp = myHead; for(int i = iMySize - 1; i >= 0; i--) { data[i] = temp.getData(); temp = temp.getNext(); } temp = myHead; for(int i = 0; i < iMySize; i++) { temp.setData( data[i] ); temp = temp.getNext(); } } recursive if(iMySize > 1) { helper(null, myHead); Node temp = myHead; myHead = myTail; myTail = temp; } public void helper(Node prev, Node curr) { //base case, fallen off the list, nothing to do if( curr != null ) { helper( curr, cur.getNext() ); curr.setNext(prev); } }