Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Computer Science Final Exam (CS 61B) at University of California, Berkeley - Spring 2005, Exams of Data Structures and Algorithms

The final exam for cs 61b (data structures) at the university of california, berkeley for the spring 2005 semester. The exam covers topics such as data structures, augmented trees, asymptotic and amortized analysis, sorting, splay trees, 2-3-4 trees, and augmented binary search trees.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of California at Berkeley
Department of Electrical Engineering and Computer Sciences
Computer Science Division
Spring 2005 Jonathan Shewchuk
CS 61B: Final Exam
This is an open book, open notes exam. Electronic devices are forbidden on your person, including cell
phones and laptops. Please put them on the desk at the front of the room (and turn your cell phone off) or
risk losing a point. Do not open your exam until you are told to do so!
Name:
Login:
Lab TA:
Lab time:
Do not write in these boxes.
Problem # Possible Score
1. Miscellany 8
2. Data structures 6
3. Sorting 12
4. Augmented trees 5
5. Asymptotic analysis 6
6. Amortized analysis 6
7. Index sort 7
Total 50
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Computer Science Final Exam (CS 61B) at University of California, Berkeley - Spring 2005 and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of California at Berkeley Department of Electrical Engineering and Computer Sciences Computer Science Division

Spring 2005 Jonathan Shewchuk

CS 61B: Final Exam

This is an open book, open notes exam. Electronic devices are forbidden on your person, including cell phones and laptops. Please put them on the desk at the front of the room (and turn your cell phone off) or risk losing a point. Do not open your exam until you are told to do so!

Name:

Login:

Lab TA:

Lab time:

Do not write in these boxes. Problem # Possible Score

  1. Miscellany 8
  2. Data structures 6
  3. Sorting 12
  4. Augmented trees 5
  5. Asymptotic analysis 6
  6. Amortized analysis 6
  7. Index sort 7

Total 50

Problem 1. (8 points) A Miscellany.

a. (2 points) Consider the following method to make all the nodes in a doubly-linked list (circularly linked, with sentinel) available for garbage collection. Recall that head references the sentinel. As- sume that listnodes are referenced only by other listnodes in the same list.

public void garbageList() { if (size > 0) { head.next.prev = null; //? Erase references to sentinel. head.prev.next = null; //? } head.next = null; //? Erase references to other nodes. head.prev = null; //? head = null; }

Are the lines marked with question marks necessary to ensure that all the listnodes are available to be garbage collected? Explain.

b. (1 point) Recall that a SimpleBoard object stores an 8 × 8 array grid in which each cell has value 0, 1, or 2. Suppose you want to store lots of SimpleBoards in a hash table. Can you think of a reason why the following hash code will not distribute SimpleBoard objects evenly among the buckets? (Assume the compression function is good.)

public class SimpleBoard { private int[][] grid;

public int hashCode() { int code = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { code = code + (i + j) * grid[i][j]; } } return code; } }

Problem 2. (6 points) Operations on Data Structures.

a. (2 points) What does the following binary heap look like after insert(1)?

Fill in your

answer here

b. (2 points) Draw the following splay tree after execution of the operation remove(6).

c. (2 points) Draw the following 2-3-4 tree after execution of the operation insert(30).

Problem 3. (12 points) Sorting.

a. (3 points) Show how array-based quicksort sorts the array 5 9 7 4 0 2 8 8. Always choose the last key in an array (or subarray) to be the pivot. Draw the array once for each swap.

b. (1 point) Consider a list-based quicksort that computes the average (mean) value i of all the keys in the list, then chooses the pivot to be i + 1. As usual, we partition the list into three separate lists: keys less than i + 1, keys equal to i + 1 (of which there might be none), and keys greater than i + 1. We sort the first and last lists recursively, then concatenate the three lists together. What’s wrong with this algorithm?

c. (2 points) Consider the following sorting algorithm. Loop through the list of input items, inserting each item in turn into an ordinary binary search tree. Then, perform an inorder traversal of the tree, and output the items in inorder. The worst-case running time of this algorithm is in Θ( ). The best-case running time of this algorithm is in Θ( ).

d. (2 points) Describe how to modify the tree in part (c) so that the algorithm does both of the following.

  • it runs in O(n log n) worst-case time, and
  • it runs in O(n) time if the input array is already sorted. Explain why it runs in O(n) time in the latter case.

Problem 4. (5 points) Augmented Binary Search Trees.

The rank of a key k in a set S is the number of keys in S that are less than or equal to k (including k itself). We want to store S in an ordinary binary search tree so that we can determine the rank of any key in it in O(h) time, where h is the height of the tree. Note that because S is a set, there are no duplicate keys in the tree. Note also that you should not perform rotations or otherwise restructure the tree.

a. (1 point) What information should you store in each node of the tree?

b. (2 points) Let k be a key in S. Describe an algorithm that determines the rank of k in O(d) time, where d is the depth of k in the tree.

c. (2 points) Describe an algorithm that inserts a new key k in O(h) time, where h is the height of the tree. If k is already in the tree, your algorithm should change nothing (as duplicate keys are not allowed). Otherwise, your algorithm must make all necessary adjustments to the information stored in the tree to ensure that your algorithm in part (b) still works.

Problem 5. (6 points) Asymptotic Analysis.

a. (2 points) Rewrite the expression O(8x log 7 y

4 8 + max{^3 y (^4) log^9 y, 5 √x} + 2 2 y (^) + 10x sin x) in the simplest possible form. (No proof is required. Assume x ≥ 2 and y ≥ 2 .)

b. (4 points) Prove formally and rigorously, omitting no details, that x^2 + 10xy + y^2 ∈ O(x^2 + y^2 ). (Both x and y are positive variables that can grow arbitrarily large.)

Problem 7. (7 points) Index Sort.

Write a method indexSort that uses the following algorithm to sort an array x of Thing objects. Loop through the Things in x. For each Thing, calculate what index it will be stored at in the output array y. Obviously, this index depends on the keys of the other Things in the array x. Copy the Thing’s reference from x to y. Note that x may contain duplicate keys, and it is your responsibility to make sure your sort is stable. Your sort should run in O(n^2 ) time, where n is the length of x. For full marks, each Thing’s reference should be copied only once ever. Do not change the array x.

public class Thing { int key; // Use the "key" field as the sort key. Object value;

public static Thing[] indexSort(Thing[] x) { // Returns sorted copy of x. Thing[] y; // Don’t forget to construct a new array.

return y; ___ } Check here if your answer | | } is continued on the back. |___|