



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
Material Type: Exam; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Unknown 2008;
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




(a) The essential property of a good user interface is that it is easy for programmers to implement. False
(b) DFS takes O(|E| + |V |) time if the graph is represented as an adjacency matrix. |E| is the number of edges and |V | is the number of vertices(nodes). False
(c) The worst-case performance of looking up an element in a hash table is O(n), where n is the number of elements. True
(d) Binary search trees are faster than hash tables in practice. False
(e) Trees and singly-linked lists are both DAGs. True
(f) Binary search requires linear time in the worst case.
False
(g) Insertion sort has worst-case running time that is O(n lg n). False
(h) Swing Listeners are an example of the Observer pattern. True
(i) In the worst case, both breadth-first and depth-first search can require state that is O(|V |) in size. True
(j) Breadth-first search uses a stack. False
(a) [4 pts] You have a large collection of objects representing vehicles with New York State license plates. You need to be able to look up a vehicle given its license plate. X Hash table Linked list Tree Binary search tree Array Resizable array
(b) [4 pts] You want to keep track of appointments and other events organized by their time and date. It is important to be able to efficiently find all the events between a starting time/day and an ending time/day. Hash table Linked list Doubly-linked list Binary heap X Binary search tree Array
(c) [4 pts] You are writing a program that watches packets going by on the network and records the IP address of the host machine sending each packet, for later processing. It’s important that your program not pause for any significant amount of time because it might miss some packets. Hash table X Linked list Graph Binary search tree Array Resizable array
(d) [4 pts] You are implementing an interactive program development environment (like Eclipse) for the Java language. You need a data structure that keeps track of all the classes and packages and lets you find, for example, the set of classes in a given package or its subpackages, or classes nested inside a given class. Linked list X Tree Binary search tree Binary heap Array Resizable array
(a) [4 pts] Consider a perfectly balanced binary tree of height h. What is the largest possible number of nodes it can contain? Be precise. Answer: 2 h+1^ − 1_. Recall that the height of a tree is the length of the longest path from the root to any leaf._ (b) [8 pts] Consider the function f (n) = a lg(n + b), where a and b are positive constants. Show that this function is O(lg n). ( Hint: consider n ≥ max(b, 2)) Answer: We need to show that there exists some k and n 0 such that a lg(n + b) ≤ k lg n for all n ≥ n 0_. Consider_ n 0 = max(b, 2) and k = 2a_. Then_ lg(n + b) ≤ lg 2n = 1 + lg n_. So_ a lg(n + b) ≤ a lg n + a , and since 1 ≤ lg n , this is bounded by 2 a lg n = k lg n , as desired. (c) [6 pts] (*) Let us say that a binary search tree is roughly balanced if the depth of the deepest node is no more than twice the depth of the least deep node that is missing one or both of its children (i.e., it has null instead of a child). Show that in such a tree (a red-black tree is one example), binary search takes time O(lg n), where n is the number of nodes in the tree. You may take the result of part (b) as given. ( Hint: how many nodes have a depth less or equal to h/ 2 ?) Answer: Binary search takes time that is O(h). So we need to show that h is O(lg n). Consider the set of nodes that are at a depth of h/ 2 or less. These nodes form a perfectly balanced tree, and therefore there must be 2 h/2+1^ − 1 of them. Therefore, we know that 2 h/2+1^ − 1 ≤ n , so h ≤ 2 lg(n + 1) − 2_. But by part (b),_ 2 lg(n + 1) − 2 is O(lg n) , so h is too.
/* A Date is a day of a unspecified year. For example, May 17 or
(a) [3 pts] Which methods are creators? Which are observers? Which are mutators? Is this a mutable or an immutable data abstraction? Answer: Creators: Date , tomorrow , yesterday_. Observers:_ month , day , tomorrow , yesterday , toString Mutators: none This is an immutable data abstraction. (b) [4 pts] The methods tomorrow and yesterday fail to specify some behavior, and furthermore, they don’t consider the possibility of leap years. Change the signature and specification of the tomorrow method to correct these problems. (You have some flexibility here; all reasonable solutions will be ac- cepted). Answer: /** Creates a new Date representing the next day of the year. Requires that the current day is not December 31. For February 28, the next day is February 29 if the year is a leap year, March 1 otherwise. @param leap_year whether the year is a leap year. */ public Date tomorrow(boolean leap_year) { ... } Some people simply assumed that it was a leap year. But this violated the original intent of the data abstraction, so it wasn’t a good solution. (c) [2 pts] Now, suppose we want to implement this class with just a single integer field representing the number of days since January 1, counted according to a leap year. // The number of days elapsed since the beginning of a leap year, // inclusive. Thus, January 1 is represented by 1 and December 31, by 366. private int day_count; What, if anything, is the representation (data structure) invariant for this class? Answer: The invariant is that day count must be between 1 and 366.
(d) [5 pts] Implement the method tomorrow using the representation of part 5(c) and your specification. You may use existing methods and you may define additional private methods and private constructors. Answer: private Date(int c) { day_count = c; } public Date tomorrow(boolean leap_year) { Date ret = new Date(day_count + 1); if (!leap_year && day_count == 59) ret.day_count = 61; return ret; } If you made December 31 wrap around in tomorrow() , you needed to check for that too. (e) [6 pts] (*) Suppose we wanted to define a subclass of Date that also kept track of the year, called YearDate. We could add a field for that: class YearDate extends Date { private int year; ... } Show how to implement the following without changing the superclass:
We are using this list data structure:
class ListNode