Test 1, Compsci 100, Spring 2009 1. In each row there are 2N-1 triangles. The last triangle in row N has value N^2. You can see this by pattern and generalizing, you could know that the sum of the first N odd numbers is N^2, or you could prove it. A. seventh row, last triangle is 49 B. 40th row, last triangle is 40*40 = 1600 C. 61st row, left-most/first triangle is 1+last-in-60th row = 3601 You can also get this by using the general formula N^2 - (2N-1) + 1 or 61*61 - (2*61-1) + 1 = 3721 - 121 + 1 = 3601 D. answer is 2N-1 which is O(N) E. if there are N^2 total triangles there are N rows, so in the last row there are 2N-1 triangles which is O(N) F. Similarly to E. we take square root, so O(N^2) G.1 combine(N) returns a pyramid with twice as many rows as N, or 2N rows so # in bottom row is 2(2N)-1 which is O(N) G.2 combine(combine(N)) returns a pyramid with 4N rows (see G.1) the biggest triangle has value (4N)^2 which is 16N^2 or O(N^2) G.3 execute p = combine(p) 10 times, doubling # rows each time, yields 2^10 x original number of rows which is 10. So # rows is 10x2^10, square this to get 100x2^20 Problem 2: A. Remove first and add to last are both O(1) for a linked list. The timing loop executes O(N) times for an N-element list so we expect linear growth which is basically what's seen, constant difference between entries in the table (about .04 to .05 with some timing variances) B. For an array the times grow quadratically, that is double # elements, quadruple time, triple number of elements, time goes up by 3^2 = 9. So, for 40,000 elements we can do 2*20,000 and get 4*6.442 or we can do 4*10,000 elements and get 16*1.617 both answers are roughly 26, something close to that is good enough For 100,000 elements we can do 10*1000 so we get 100*1.617 = 161.7 or we can do 5*20,000 so we get 25*6.442 = 161 so any answer around 161 will work. C. The granularity of a millisecond timer would probably not yield valid results, so time more things to make the timing valid. D. public Node firstToLast(Node list) { if (list == null) return null; if (list.next == null) return list; Node end = last(list); Node second = list.next; end.next = list; list.next = null; return second; } 3.A: Use an ArrayList because we don't know a priori the # elements that will be returned, so add to a growable arraylist and then convert that to an array. This is much simpler/easier to code than creating an array big enough to hold all the elements, then tracking the number actually stored and returning a new array. B: .trim() removes leading/trailing spaces, not internal spaces. Sorting will move all the spaces to the front because a ' ' is less than all alphabetical characters. C: The .equals method for inner class Ana is inconsistent with .hashCode and thus won't work. Change .equals to return sortedWord.euqlas(other.sortedWord); This will ensure that words are hashed/compared anagramatically rather than lexicographically. See compareTo for how this would work in a TreeSet D: public int compareTo(Inner o){ int diff = last.compareTo(o.last); if (diff != 0) return diff; return first.compareTo(o.first); } public Strintg toString(){ return first + " " + last; } 4. A: Fix by adding the if statement below. You can also use .contains instead of .indexOf, or you could write the loop yourself, but the loop that's in .contains or .indexOf saves writing-time. public int hateCount(String name){ int total = 0; for (String friend : myFriends.get(name)){ if (myFriends.get(friend).indexOf(name) < 0) { total++; } } return total; } 4 B. public String mostLikedBy(){ String most = null; int maxLiked = 0; for(String s : myFriends.keySet()){ int count = 0; for(ArrayList list : myFriends.values()){ if (list.indexOf(s) >= 0){ count++; } } if (count > maxLiked){ maxLiked = count; most = s; } } return most; } Alternative: public String mostLikedBy(){ String most = null; int maxLiked = 0; Map map = new HashMap(); for(String s : myFriends.keySet()){ int count = 0; for(String friend : myFriends.get(s)){ if (! map.containsKey(friend)){ map.put(friend,0); } map.put(friend, map.get(friend)+1); if (map.get(friend) > maxLiked){ maxLiked = map.get(friend); most = s; } } } return most; } 5: public String mostPopular(String[] list){ Map map = new HashMap(); String maxs = null; int max = 0; for(String s : list){ String[] top = s.split(","); for(int k=0; k < 3; k++){ if (!map.containsKey(top[k])){ map.put(top[k],0); } map.put(top[k],map.get(top[k])+3-k); } } for(String s : map.keySet()){ int val = map.get(s); if (val == max && s.compareTo(maxs) < 0){ maxs = s; } else if (val > max){ maxs = s; max = val; } } return maxs; }