






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The second midterm exam for the course CS61B: Data Structures at UC Berkeley. The exam consists of 9 questions worth a total of 240 points and is to be completed in 110 minutes. The exam is closed book, except that you are allowed to two double-sided written cheat sheets. The exam covers topics such as BST, 2-3 trees, and LLRB. tips for taking the exam and instructions for filling out the exam sheet.
Typology: Exams
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Optional. Mark along the line to show your feelings Before exam: [____________________☺]. on the spectrum between and ☺. After exam: [____________________☺]. UC Berkeley – Computer Science CS61B: Data Structures Midterm # 2 , Spring 2018 This test has 9 questions worth a total of 240 points and is to be completed in 110 minutes. The exam is closed book, except that you are allowed to two double sided written cheat sheets (can use front and back on both sheets). No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided. Write the statement out below in the blank provided and sign. You may do this before the exam begins. “I have neither given nor received any assistance in the taking of this exam.” Signature: ___________________________
0 1 6 14 1 24 7 46 2 28 8 46 3 32 9 30 4 0 5 20 TOTAL 240 Name: __________________________ SID: ___________________________ Three-letter Login ID: _________ Login of Person to Left: _______ Login of Person to Right: ______ Exam Room: _____________________ Tips:
UC BERKELEY Login: _________
0. So it begins ( 1 point). Write your name and ID on the front page. Write the exam room. Write the IDs of your neighbors. Write the given statement and sign. Write your login in the corner of every page. Enjoy your free point ☺. 1. Tree time. a) ( 4 points). Suppose we have the BST shown below. Give a valid tree that results from deleting “ 7 ” using the procedure from class (a.k.a. Hibbard deletion). Draw your answer to the right of the given tree in the box. b) ( 4 points). Give an example of a rotation operation on the original tree from 1a (on the left) that would increase the height. You do not need to draw the tree , just write the operation, e.g. “rotateRight(11)”. c) ( 4 points). Draw the 2-3 tree that results from inserting 1, 2, 3, 7 , 8 , 9 , 5 in that order. d) ( 3 points). Draw the LLRB that results from inserting 1, 2, 3, 7 , 8 9 , 5 in that order. Write the word “red” next to any red link.
UC BERKELEY Login: _________ b) (4.5 points). Next to the calls to get, write the return value of the get call. Assume that get returns null if the item cannot be found. FourBucketHashMap fbhm = new FourBucketHashMap<>(); XList firstList = XList.of(1, 2, 3); fbhm.put(firstList, “cat”); fbhm.get(XList.of(1, 2, 3)); _____________________ firstList.addLast(0); // list is now [1, 2, 3, 0] fbhm.get(firstList); _____________________ fbhm.get(XList.of(1, 2, 3)); _____________________ c) (10.5 points). Next to the calls to get, write the return value(s) of the get call. Assume that get returns null if the item cannot be found. FourBucketHashMap fbhm = new FourBucketHashMap<>(); XList firstList = XList.of(1, 2, 3); fbhm.put(firstList, “cat”); firstList.addLast(1); // list is now [1, 2, 3, 1] fbhm.get(firstList); _____________________ fbhm.get(XList.of(1, 2, 3)); _____________________ fbhm.get(XList.of(1, 2, 3, 1)); _____________________ fbhm.get(XList.of(3, 4)); _____________________ fbhm.put(firstList, “dog”); fbhm.get(firstList); _____________________ fbhm.get(XList.of(1, 2, 3)); _____________________ fbhm.get(XList.of(1, 2, 3, 1)); _____________________ d) ( 4 points). What are the best and worst case get and put runtimes for FourBucketHashMap as a function of N, the number of items in the HashMap? Don’t assume anything about the distribution of keys. .get best case: _Θ__________ .get worst case: _Θ__________ .put best case: _Θ__________ .put worst case _Θ__________ e) ( 4 points). If we modify FourBucketHashMap so that it triples the number of buckets when the load factor exceeds 0.7 instead of always having four buckets, what are the best and worst case runtimes in terms of N? Don’t assume anything about the distribution of keys. .get best case: _Θ__________ .get worst case: _Θ__________ .put best case: _Θ__________ .put worst case _Θ__________ As noted on the front page, throughout the exam you should assume that a single resize operation on any hash map takes linear time.
Login: _______ 3. Weighted Quick Union. a) ( 10 points). Define a “fully connected” DisjointSets object as one in which connected returns true for any arguments, due to prior calls to union. Suppose we have a fully connected DisjointSets object with 6 items. Give the best and worst case height for the two implementations below. The height is the number of links from the root to the deepest leaf, i.e. a tree with 1 element has a height of 0. Give your answer as an exact value. Assume Heighted Quick Union is like Weighted Quick Union, except uses height instead of weight to determine which subtree is the new root. Best Case Height Worst Case Height Weighted Quick Union Heighted Quick Union b) ( 8 points). Suppose we have a Weighted Quick Union object of height H. Give a general formula for the minimum number of objects in a tree of height H as a function of H. Your answer must be exact (e.g. not big theta). c) ( 6 points). Draw a Quick Union tree of 6 objects or fewer that would be possible for Heighted Quick Union, but impossible for Weighted Quick Union. If no such tree exists, simply write “none exists.” d) ( 8 points). Create a set for storing SimpleOomage objects Assume that hashCode for SimpleOomage is the perfect hashcode you were expected to write in HW3, where hash code values are unique and always between 0 and 140,607, inclusive. public class SimpleOomageSet { private WeightedQuickUnionUF wq = new WeightedQuickUnionUF(____________); public void add(T item) {
} public boolean contains(T item) {
} } // reminder: WeightedQuickUnionUF methods are union() and connected() 4. PNH ( 0 points). This 1996 simulation video game by Maxis had a hidden feature introduced secretly by a programmer, where on certain dates of the year, “muscleboys in swim trunks” would appear by the hundreds and hug and kiss each other.
Login: _______ c) ( 6 points). Rather than implementing an entirely new data structure from scratch, we might consider using delegation or extension to implement Multiset. Which is better, delegation or extension? If delegation, what class should you delegate to? If extension, what class should you extend? If applicable, provide generic types. Fill in one of the bubbles and the corresponding blank below. ○ Delegation to an instance of the __________________________________ class is better. ○ Extending the __________________________________ class is better. 6. Min Heaps ( 1 4 points). Consider the min heap below, where each lette represents some value in the tree. For each question, indicate which letter(s) correspond to the specified value. Assume each value in the tree is unique. a) Assuming values are inserted into the heap in increasing order , indicate all letters which could represent the following values: Smallest value: □A □B □C □D □E □F □G □H □K □L □M Median value: □A □B □C □D □E □F □G □H □K □L □M Largest value: □A □B □C □D □E □F □G □H □K □L □M b) Assuming values are inserted into the heap in decreasing order , indicate all letters which could represent the following value: Smallest value: □A □B □C □D □E □F □G □H □K □L □M c) Assuming values are inserted into the heap in an unknown order , indicate all letters which could represent the following values: Median value: □A □B □C □D □E □F □G □H □K □L □M Largest value: □A □B □C □D □E □F □G □H □K □L □M
UC BERKELEY Login: _________ 7. Iteration. a) ( 12 points). Fill in the toList method. It takes as input an Iterable, where T is a generic type argument, and returns a List. If any items in the iterable are null, it should throw an IllegalArgumentException. You should use the for each notation. Do not use .next and .hasNext explicitly. public class IterableUtils { public static List toList(Iterable iterable) {
for (_______________________) { if (___________________) {
}
}
} } // assume any classes you need from java.util have been imported b) ( 8 points). The ReverseOddDigitIterator implements Iterable, and its job is to iterate through the odd digits of an integer in reverse order. For example, the code below will print out 77531. ReverseOddDigitIterator rodi = new ReverseOddDigitIterator(12345770); for (int i : rodi) { System.out.print(i); } Write a JUnit test that verifies that ReverseOddDigitIterator works correctly using your toList method from before. Use the List.of method, e.g. List.of(3, 4, 5) returns a list containing 3 then 4 then 5. import org.junit.Test; import static org.junit.Assert.*; public class TestRODI { @Test public void testRODI() { ReverseOddDigitIterator odi = new ReverseOddDigitIterator(__________);
} } // assume any classes you need from java.util have been imported
UC BERKELEY Login: _________ 8. Asymptotics a) ( 12 points). Give the runtime of the following functions in Θ notation. Your answer should be a function of N that is as simple as possible with no unnecessary leading constants or lower order terms. Don’t spend too much time on these! _Θ_____ public static void g 1 (int N) { for (int i = 0; i < NNN; i += 1) { for (int j = 0; j < NNN; j += 1) { System.out.print("fyhe"); } } } _Θ_____ public static void g 2 (int N) { for (int i = 0 ; i < N; i += 1 ) { int numJ = Math.pow(2, i + 1) - 1; // <-- constant time! for (int j = 0; j < numJ; j += 1) { System.out.print("fhet"); } } } _ Θ_____ public static void g 3 (int N) { for (int i = 2; i < N; i *= i) {} for (int i = 2; i < N; i++) {} } b) ( 4 points). Suppose we have an algorithm with a runtime that is Θ(N^2 log N) in all cases. Which of these statements are definitely true about the runtime, definitely false, or there is not enough information (NEI)? O(N^2 log N) (^) ○ True ○ False ○ NEI Ω(N^2 log N) (^) ○ True ○ False ○ NEI O(N^3 ) (^) ○ True ○ False ○ NEI Θ(N^2 log 4 N) (^) ○ True ○ False ○ NEI c) ( 6 points). Suppose we have an algorithm with a runtime that is O(N^3 ) in all cases. There exists some inputs for which the runtime is Θ(N^2 ) (^) ○ True ○ False ○ NEI There exists some inputs for which the runtime is Θ(N^3 ) (^) ○ True ○ False ○ NEI There exists some inputs for which the runtime is Θ(N^4 ) (^) ○ True ○ False ○ NEI The worst case runtime is O(N^3 ) (^) ○ True ○ False ○ NEI The worst case runtime has order of growth N^3 ○ True ○ False ○ NEI
Login: _______ d) (12 points). Give the best and worst case runtime of the following functions in Θ notation. Your answer should be as simple as possible with no unnecessary leading constants or lower order terms. Don’t spend too much time on these! Assume K(N) runs in constant time and returns a boolean. public static void g 4 (int N) { if (N == 0) { return; } g 4 (N - 1); if (k(N)) { g 4 (N - 1); } } Best case: _Θ_______ Worst case: _Θ_______ public static void g 5 (int N) { if (N == 0) { return; } g 5 (N / 2); if (k(N)) { g 5 (N / 2); } } Best case: _Θ_______ Worst case: _Θ_______ e) ( 6 points). Give the best and worst case runtime of the code below in terms of N, the length of x. Assume HashSets use the idea of external chaining with resizing used in class, and that resize is linear. public Set uniques(ArrayList x) { HashSet items = new HashSet<>(); for (int i = 0; i < x.size(); i += 1) { items.add(x.get(i)); } return items; } Best case runtime for uniques: _ Θ_____ Worst case runtime for uniques: __Θ_____ f) ( 6 points). Consider the same code from part b, but suppose that instead of Planets, x is a list of Strings. Suppose that the list contains N strings, each of which is length N. Give the best and worst case runtime. Best case runtime for uniques: _ Θ_____ Worst case runtime for uniques: __Θ_____