






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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A final examination for the course E&CE 250 - Algorithms and Data Structures at the University of Waterloo. The exam consists of two problems, each with two parts. The first problem covers algorithm analysis and solving recurrences, while the second problem covers queues and primary clustering. The exam is closed book and no calculators are allowed. Java code fragments and figures to aid in problem-solving.
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!
Final Examination Instructors: R.E.Seviora and L.Tahvildari 3 hrs, Apr. 11, 2001
Name: Student ID:
Do all problems. The number in brackets denotes the relative weight of the problem (out of 100). If information appears to be missing from a problem, make a reasonable assumption, state it and proceed. If the space to answer a question is not sufficient, use the last (overflow) page. Closed book. No calculators allowed.
1.Consider the algorithm represented by the following Java program fragment. What value does bar compute? Derive a tight, big oh expression for the running time of the method bar.
static int bar (int x, int n) { int sum = 0; for (int i = 1; i <= n; ++i) sum = sum + i; return {x + sum}; }
2.Consider the algorithm represented by the following Java program fragment. The method bar computes the same function as bar above. However, bar1 is based on a different algorithm and its tight big oh bound is O(n). What value does foo compute? Give a tight, big oh expression for the worst case running time of the method foo.
static int foo (int x, int n) { int sum = 0; for (int i = 1; i <= n; ++i) sum = sum + bar1 (i, n); return {x + sum}; }
Solve the following recurrence. You may assume that n is a power of 2. Show all your work.
The interface Queue discussed in the lectures contained only the operations enqueue , dequeue and getHead. In some applications, the method reverse() is required. This method will reverse the order of items in the queue. For example, if the original queue contained a, b, c, d (in this order), reverse() would reorder its contents to d, c, b, a. In this problem, you are asked to devise an algorithm for reverse() , for the case of linked list implementation of the queue. Note that you may use any of the methods of the LinkedList class in your answer.
public class QueueAsLinkedList extends AbstractContainer implements Queue { protected LinkedList list;
// standard methods of Queue interface … // reverse () : reverses the order of items on the queue public void reverse () {
A. Hash/Scatter Table
1.Consider a hash table with separate chaining with ten hash locations. Using the hash function h(x) = x mod 10, insert the keys {33, 54, 69, 74, 18, 19} (in the order given) into the hash table. Draw the resulting hash table. (Note: to keep this example simple, we use table size that is not prime.)
0 1 2 3 4 5 6 7 8 9
B. Primary Clustering
Figure 1 illustrates an open addressing scatter table with linear probing, as it is being filled, at increments of 10 percent of table capacity. The black bands in this figure represent clusters. A cluster is a set of adjacent occupied entries in the table. Figure 2 illustrates the table being filled under the same scenario. However, in this case, double hashing is used.
Figure 1 – Linear Probing Clusters
Figure 2 – Double Hashing Clusters
B. Space Requirements of Trees
Derive an expression for the total space needed to represent a tree of n internal nodes using each of the following classes. Assume four-byte integers and references. The expression should include the space requirement of the tree only, and not include the space needed to store the objects referred to by the nodes of the tree.
// … }
// … }
A. Search Trees
node depth height balance factor A B C D E F G
B. Binary Trees and Heaps
public class BinaryTree extends AbstractTree { public Boolean isHeap () { if (isEmpty ()) return true; if (!getLeft ().isEmpty () || getLeft ().getKey ().isLT (getKey ())) return false; if (!getRight ().isEmpty () || getRight ().getLey ().isLT (getKey ())) retuen false; return getLeft ().isHeap () && getRight ().isHeap (); } }
(b) What is the height of a perfect binary tree with n internal nodes?
OVERFLOW SHEET [Please identify the question(s) being answered.]