Your Name: CompSci-61B, Data Structures Final Exam Your 8-digit Student ID: Your CS61B Class Account Login: This is a final test for mastery of the material covered in our labs, lectures, and readings. It consists of 11 questions worth a total of 100 points. Circle the correct answer for the multiple-choice questions, and write in the space provided for the essay questions. This exam is open book and open note. Partial credit is possible. 1. Write a class file for compsci61b.mystack, extending your compsci61b.myarraylist class, with the following changes: [12 points] add O(1) versions of public boolean push(t value), public T pop(), and public T peek(). iterate from most recently added value to oldest value pop order (that is, FILO) throw UnsupportedOperationExceptions (with any text you want) for the 2 add methods, remove, and the iterator s remove. Here is a list of the methods in compsci61b.myarraylist: public MyArrayList() public boolean add(t value) public boolean add(int, T value) public T remove(int index) public void clear() public boolean replace(int index, T value) public T getentry(int index) public boolean contains(t value) public int size() public boolean isempty() public boolean isfull() public String tostring() public Iterator<T> iterator() -- should be in FILO order package compsci61b; public class MyStack<T> extends MyArrayList<T> Write your code here: CompSci-61B Midterm Exam, page 1 continued on next page
Write your code here (continued): CompSci-61B, Data Structures Final Exam CompSci-61B Midterm Exam, page 2
2. Write a class file for compsci61b.myprorityqueue, extending your compsci61b.mysortedlinkedlist class, with the following changes: [10 points] add public T poll() and public T peek(). ( poll should return the smallest value in the queue, or null if the queue is empty) throw UnsupportedOperationExceptions (with any text you want) for the add methods, remove, and the iterator s remove. Here is a list of the methods in compsci61b. MySortedLinkedList: public boolean remove(t value) public boolean add(t value) public boolean contains(t value) public boolean add(int index, T value) unsupported operation throws an exception public boolean replace(int index, T value) unsupported operation throws an exception public T getentry(int index) public boolean contains(t value) public int size() public boolean isempty() public boolean isfull() public String tostring() public void clear() public Iterator<T> iterator() should be in order smallest to largest package compsci61b; public class MyStack<T> extends MyArrayList<T> Write your code here: CompSci-61B Midterm Exam, page 3
3. Rewrite MySortedArrayList.add so that it has a best case of O(1): [6 points] public boolean add(t value) // the original version, O(n) efficiency if (value == null) return false; if (nvalues == data.length) doublethearraysize(); // find the insertion location int index = 0; for (; index < nvalues; index++) if (value < data[index]) break; // shift (if necessary) and insert for (int i = nvalues; i > index; i--) data[i] = data[i - 1]; // move right data[index] = value; // copy into array ++nvalues; return true; public boolean add(t value) // the rewritten version with an O(1) best case Write your code here: CompSci-61B Midterm Exam, page 4
4. Imagine that you are grading the programming exercise on binary search trees for a future CS61B class and to make your job easier, you decided program a method "isbst" that would tell you whether the binary trees constructed by your students are actually Binary Search Trees. That is, isbst should return true if the input Binary Tree satisfies the Binary Search Tree property and false if not. (Assume that the input is a valid binary tree we just want to see if it is also a binary search tree.) These trees do not implement generics they store Integer values. Assume that you would copy/paste this method into the students submitted source code, and place calls to the method in the students main test code. All students name the private inner node class the same, per the lab specifications. Your program must run in time linear to the size of the input tree. [15 points]...package and import statements... public class MyBinaryTree // per the specification private class Node // per the specification private Integer value; // per the specification private Node left; // per the specification private Node right; // per the specification...possibly methods here......student s code (not you!)... public boolean isbst(node tree) // copy/paste this into student s code Write your code here: CompSci-61B Midterm Exam, page 5 continued on next page
Write your code here (continued): CompSci-61B, Data Structures Final Exam...student s code (not you!), including main test method... CompSci-61B Midterm Exam, page 6
5. Explain the big oh efficiency for "contains" for each of the following (Circle one each) [6 points total] unsorted array: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] sorted linked list: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] balanced binary tree: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] chained hash table: (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) [1.5 points] 6. Regarding hash tables: [15 points total] a. While using a hash table with chaining and no maximum load factor, you found out that you inserted too many elements into the table. To be more precise, you actually inserted n log n elements into the hash table whose array size is n. (a) What would be the average lookup time for your hash table now? (b) What if you doubled the size of the Hash Table and rehashed the same elements, what would be the average lookup time now? Assume that you can insert however many elements you want in whatever order you want using whatever hash function you want. Express the answers in big oh notation. [4 points] Answer here: a. b. b. You have two hash tables of the same size, and they are hashing the same set of keys using the same hash function. However, one hash table uses linear probing and one uses chaining. Suppose you can insert however many elements you want in whatever order you want, is it possible for one hash table to have, on average, linear time lookup speed while the other has, on average, constant time lookup speed? Explain. [7 points] Answer here: c. In our implementation of linear probing in hash tables, our array elements could be entry references, null, or blank. What is the purpose of using blanks instead of nulls? [4 points] Answer here: CompSci-61B Midterm Exam, page 7
7. Specify which data structure you would use in each of these scenarios. Choose from one of the following. If multiple answers are possible, choose the most efficient one. Briefly explain your choices. [9 points total] Queue Stack Priority Queue Binary Search Tree Graph Hash Table a. You're simulating a keyboard and would like to process the keystrokes and then print them on the screen. [1.5 points] b. You need to process the service events for lab 8 but now, the events all take the same amount of time to complete. [1.5 points] c. You're writing a graph traversal program and you would like to use a separate additional data structure to keep track of whether a vertex has been visited or not. [1.5 points] d. You're writing expression parsing program for an early HP calculator that uses reverse polish notation. [1.5 points] e. You want to be able to input the prices of many different cars, and then find cars that are neither too expensive nor too cheap for your customers. [1.5 points] f. You would like to store all possible board configurations for the game of chess and you also want to store what moves you can make to reach one configuration from another. [1.5 points] CompSci-61B Midterm Exam, page 8
8. Suppose we modify the depth-first traversal algorithm for a graph to push the node into a stack before traversing its neighbors and then pop the stack at the end, in accordance with the following pseudocode: boolean traverse(vertex v) stack.push(v);... pre-process v... for-all Vertex u where (v, u) is an edge if (!stack.contains(v)) traverse(u); stack.pop();... post-process v... Answer the following 2 questions Provide examples: [7 points total] a. Will this modified depth-first search algorithm work on a directed graph (digraph), ie. have the same behavior as the normal depth-first search? [5 points] b. Will it work on an undirected graph? [2 points] CompSci-61B Midterm Exam, page 9
9. Write a static method which is supposed to take an array of ints between 0 and m inclusive, and count the number of repetitions in the array. The array and m are arguments in the method s parameter list. For example, an array of 1, 3, 1, 4, 3, 3 has 3 repetitions, because the number 1 repeats once and the number 3 repeats twice. Furthermore, your program needs to run in time O(n+m) where n is the size of the array. The variable count is a hint, you don't need to use it you may cross it out -- and you can declare any variables of your own. [10 points] public static int countrepetitions(int[] arr, int m) int[] count = new int[m + 1]; Write your code here: CompSci-61B Midterm Exam, page 10
10. In a graph representation of a maze, the entry point is called A and the exit is Z. There is only one direct path through the maze (without doubling back or circling). Which traversal method(s) will find the direct route from A to Z? Assume that the maze is represented by a series of nodes at the decision points, and the edges include the distance between nodes. (Circle all that apply, and explain) [4 points] (a) depth first (b) breadth first (c) shortest route (d) cheapest route 11. In the BST shown to the right, the value I inside in the node marked 1 is to be removed. This may involve the swapping of values among nodes, and will involve the removal of one node. There are two possible solutions to this problem -- identify the changes, in order, for both: [6 points] First possible solution (fill in the blanks): Put value into node # Remove node # Second possible solution (fill in the blanks): Put value into node # Put value into node # Put value into node # Put value into node # Remove node # use as many of these blank as you need CompSci-61B Midterm Exam, page 11