You are on page 1of 7

(5 points) Consider a sorted circular doubly-linked list where the head element points to the

smallest element in the list.


(a) What is the asymptoptic complexity of finding the smallest element in the list?
(b) What is the asymptoptic complexity of finding the largest element in the list?
(c) What is the asymptoptic complexity of determining whether a given element e appears
in the list?
(d) What is the asymptotic complexity of finding the median element in the list?
(e) What is the asymptotic complexity of deleting a given element e in the list (not including
the cost of finding it)?

(5 points) Check the ADT or data structure that is most appropriate for each of
the following problems, for each answer give a short explanation of your choice.

(a) You want to build an address book with entries in alphabetical order by last
name.
Unsorted List
Sorted List
Stack
Balanced Search Tree
Directed Graph

(b) You want to build a meeting reminder for a PDA that keeps track of events
you schedule and periodically checks the next event to sound an alarm to
remind you of the next thing you need to do.
List
Hashtable
Stack
Queue
Priority Queue

(c) You want to build a table of contents for a textbook. The textbook consists
of chapters, chapters consist of sections, and sections consist of subsections.
List
Hashtable
Tree
Binary Tree
Balanced Tree

(d) You want to build an email address miner that scans a hard drive looking for
email addresses, then sends them to a remote host.
List
Hashtable
HashSet
Array
Balanced Tree

(20 points) True or false?


(a) The AVL invariant states that a tree’s shortest and longest paths differ in length by at
most 1.
(b) The rotate operation on AVL trees preserves inorder numbering.
(c) Abstract classes can be instantiated like any other class.
(d) A stack is a FIFO structure and a queue is a LIFO structure.
(e) If class A implements interface I, class B extends A, and class C extends B, then C
implements I.
(f) If class A implements interface I, class B extends A, class C extends B, and interface J
extends interface I, then C implements J.
(g) 3n is O(2n).
(h) log(3n) is O(log(2n)).
(i) log3 n is O(log2 n).
(j) log(n2) is O(log(n3)).
(k) The optimal number of collisions per bucket in a hashtable is 1.
(l) If f(n) is O(g(n)) and g(n) is O(h(n)), then f(n) is O(h(n)).
(m) An occurrence of ’this’ in a static method will cause a compile-time error.
(n) Binary search is as efficient on linked lists as on arrays, provided the list is doubly linked.
(o) The finally block in a try-catch-finally statement is executed even if no exception is
thrown and there is a return statement in the try block.
(p) A non-abstract subclass of an abstract class C must provide definitions for all the abstract
methods of C.
(q) Downcasting is always legal and can be done without an explicit cast.
(r) Every dag has a unique topological sort.
(s) In the worst case, search in an unbalanced binary tree is asymptotically
the same complexity as search in a balanced binary tree.
(t) The worst-case running time of quicksort is worse than that of mergesort

(6 points) List the sequence of nodes visited by preorder, inorder, and postorder
traversals of the following tree:
Given the following heap, draw the heap that would result after deleting
the minimum element.

(5 points) Say the following tree was obtained by inserting the element 42 into an
AVL tree. The tree no longer satisfies the AVL invariant, but the invariant can be
reestablished by performing two rotate operations. Show the resulting tree after
this is done.
Suppose we use mergesort to sort an array containing the following sequence of elements:
1, 5, 6, 3, 2, 4, 9, 0. For this example input, illustrate how merge sort works by drawing the
array states that result after each merge.

1) Consider the following algorithm described in pseudo-code, which takes an array A of n


positive integers as input and uses an initially-empty queue Q as an internal variable:
Please answer each of the following question concerning this algorithm:
1. What is the output of this algorithm for A = {1, 9, 11, 14, 5, 3, 7, 13, 3, 12, 5}?
2. Describe in one sentence what this algorithm computes.
3. Characterize, using the big-Oh notation, the running time of the above algorithm in
terms of n, the number of integers in A.

Suppose we have a hash table with an table size of 10, into which we are inserting the
integers from 1 to 10. Our hash function h(x) is the square of the element, modulo the table
size (i.e., the remainder when the square is divided by the table size. For example, h(9) = 1.)
Draw the hash table that results, assuming it uses chaining.
Problem B1
A 2-coloring of an undirected graph with n vertices and m edges is the assignment of one of
two colors (say, black or white) to each vertex of the graph, so that no two adjacent nodes
have the same color. So, if there is an edge (u; v) in the graph, either node u is black and v is
white or vice versa. Give (pseudocode!) an O(n+m) time algorithm to 2-color a graph or
determine that no such coloring exists, and justify the running time. For this problem you
are not allowed to assume the existence of any “black-box” algorithms, i.e. give pseudocode
for everything you use.

The following shows examples of graphs that are and aren't 2-colorable:

Problem B2
In this problem you will design a linear-time algorithm that clones a binary tree T . This
algorithm should construct an exact copy of T . You are only allowed to use the methods of
the Tree and Binary Tree ADTs (i.e. isEmpty, root, parent, children,
isInternal, isExternal, isRoot, size, elements, positions,
swapElements, replaceElement, leftChild, rightChild, sibling,
expandExternal, removeAboveExternal).
1. Describe (in pseudo-code) your algorithm.
2. Analyze its worst-case running time.

Problem B3
Let S1, S2, . . ., Sk be k different sequences whose elements have integer keys in the range
[0; N − 1], for some N > 1. Describe an algorithm (in pseudocode) to sort all of the sequences
in O(k + n + N) time, where n is the total size of all of the sequences. Note that the
sequences should remain separate, so your algorithm should rearrange the elements in
each sequence into the sorted order, not output one big sorted sequence containing all of
the elements.

You might also like