• No results found

CompSci-61B, Data Structures Final Exam

N/A
N/A
Protected

Academic year: 2021

Share "CompSci-61B, Data Structures Final Exam"

Copied!
11
0
0

Loading.... (view fulltext now)

Full text

(1)

Your Name: ___________________________ 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 ofpublic 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:

(2)
(3)

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:

(4)

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:

(5)

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:

(6)

Write your code here (continued):

}

(7)

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]

(8)

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]

Use__________________________ (fill in the blank with one of the above data structures) Explain:

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]

Use__________________________ (fill in the blank with one of the above data structures) Explain:

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]

Use__________________________ (fill in the blank with one of the above data structures) Explain:

d. You're writing expression parsing program for an early HP calculator that uses reverse polish notation.[1.5 points]

Use__________________________ (fill in the blank with one of the above data structures) Explain:

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]

Use__________________________ (fill in the blank with one of the above data structures) Explain:

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.

(9)

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]

(10)

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 mare 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) Write your code here:

{

(11)

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

Explain:

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 #___________ use as many of

Put value ______ into node #___________ these blank

Put value ______ into node #___________ as you need…

References

Related documents

 Presence of cancer care infrastructure such as medical laboratory, ultrasound facilities, X-ray facilities, Radio- and chemo- therapy facilities, nuclear

More specifically, in order to conduct research on revitalizing education for dynamic hearts and minds and address the issue of how to advance practice in

createQueue(java.lang.String, boolean, int)' has been decreased from public to protected com.webmethods.portal.jms.um.UmJMSClient: Accessibility of method 'public

[r]

For sustainability and other agricultural issues, such boundary-spanning organizations help build social networks that connect actors with different types of experiential and

public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException5. public Socket(InetAddress address, int port, InetAddress localAddress, int

public class ArrayStack implements Stack { public static final int CAP=1000;.. private

public boolean weakCompareAndSet(int i, long expect, long update) Atomically sets the element at position i to the given updated value if the current value == the