# Java Vector - Data Structures - Exams, Exams for Data Structures. Bhupendra Narayan Mandal University

## Data Structures

Description: Main points of this exam paper are: Java Vector, Worst Cases, Representation Consists, Current Length, Asymptotic Bounds, Right Place, Bound Means, Java Code, Functions Marked, Additional Declarations
Showing pages  1  -  4  of  13
UNIVERSITY OF CALIFORNIA
Department of Electrical Engineering
and Computer Sciences
Computer Science Division
CS61B P. N. Hilﬁnger
Fall 2000
Final Examination
Your exam should contain 7problems on 13 pages. Oﬃcially, it is worth 50 points.
This is an open-book test. You have three hours in which to complete it. You may consult
any books, notes, calculators, or other inanimate objects (other than computers) available to you.
You may use any program text supplied in lectures, problem sets, or solutions. Please write your
answers in the spaces provided in the test. Make sure to put your name, login, and lab section in
the space provided below. Put your login and initials clearly on each page of this test and on any
Read all the questions carefully to begin with, and ﬁrst try to answer those parts about which
you feel most conﬁdent.
Discussion section number or time: TA:
1. /12
2. /10
3. /10
4.
5. /5
6. /6
7. /7
TOT /50
1
1. [12 points] Answer each of the following brieﬂy. Where a question asks for a yes/no answer,
give a brief reason for the answer (or counter-example, if appropriate).
a. If f(x)Θ(x3) and g(x)O(x2), and if there is some x0such that f(x0)> g(x0), then is
f(x)> g(x) for all x > x0? Assume fand gare everywhere positive.
b. If g(x) = x2cos x, is g(x)O(x2)? Is g(x)Ω(x)?
c. A sorted list of values is maintained as a Java Vector whose initial capacity is N0. That
is, the representation consists of an array (initially of length N0) and a current size (always
less than or equal to the current length of the array), and the array is expanded by factors
of two as needed. What are the tightest asymptotic bounds you can give for the best and
worst-case times for adding N=K·N0values to this list (inserted in the right place to keep
the list ordered), assuming the list is initially empty? (The “tightest” bound means “a Θ(·)
bound if possible, and otherwise the smallest O(·) bound and largest Ω(·) bound possible.”)
We want bounds for the worst-case time and bounds for the best-case time. Include brief
descriptions of the best and worst cases.
d. Suppose that I take a sorted linked list of Nintegers, and break it into a list of Mequal-sized
sorted lists of integers (that is, put the ﬁrst N/M integers into the ﬁrst list, the next N/M
into the second, etc.). What is the worst-case time for ﬁnding whether an integer xis in
anywhere in this list of lists? If for some ﬁxed N, you can choose M, what Mshould you
choose for maximum lookup speed?
e. You are in the process of testing your project and suddenly realize that you need to ﬁnd out
when and where a certain CS ﬁnal is being held, but you have no friends in the course, you
have no written information on the subject, and the professor and TAs refuse to tell you on
the grounds that you should be able to ﬁnd out for yourself. What do you do? Please be
precise.
f. Consider a directed graph that has Vnodes and an in-degree of 2 (that is, no node has more
than two incoming edges). What is the tightest bound you can give for the worst-case time
to topologically sort this graph?
2. [10 points] The following fragment of Java code (which continues on the next page) contains
several errors in some of the functions marked ‘// ?’.
a. [6 points] Find and (neatly) correct as many as possible, making as little change as possible.
Do not correct things unless they really don’t work. WARNING: one of the marked functions
is error-free! There may be more than one correction possible; be sure to choose one that
makes the resulting program conform to the comments. Not all methods are shown; you
don’t need to add any to correct the program.
/** A Mapper represents a function from values to DLists.
* If M is a Mapper, it is applied to a value, x, with M.map (x). */
public interface Mapper {
DList map (Object x);
}
/** A DList represents a list of Objects. */
public class DList {
/* The rep field of a DList points to a sentinel node in a circularly
* linked list. Because the list is circular, the node after the
* sentinel contains the first item in the list, and the node
* before the sentinel contains the last item. For an empty DList,
* the sentinel is its own next and previous element. Example of
* list containing items A and B:
rep: A B
*/
/** An empty DList. */
public DList ()
{ rep = new DLink (null); rep.next = rep.prev = rep; }
/** True iff THIS is an empty list. */
public boolean empty () { return; } // ?
/** The first item in THIS list. */
public Object first () { return rep.next.value; }
/** The last item in THIS list. */
public Object last () { return rep.prev.value; }
/** Add X to the beginning of THIS list. */
public addFront (Object x) { // ?
rep.next.spliceAfter (item, item);
}
/** Add X to the end of THIS list. */
public addBack (Object x) { // ?