# Exam 4: Data Structures and Algorithms

Distributed: Monday, 28 April 2003
Due: 10 a.m., Monday, 5 May 2003
No extensions!

This page may be found online at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Exams/exam.04.html`.

Contents

## Preliminaries

There are five problems on the exam. Some problems have subproblems. Each full problem is worth twenty-five points. The point value associated with a problem does not necessarily correspond to the complexity of the problem or the time required to solve the problem. If you write down the amount of time you spend on each problem and the total time you spend on the exam, I'll give you two points of extra credit.

You should answer only four of the five questions on the exam. If you answer five, I will choose which four to grade.

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than that limitation, you should feel free to use all reasonable resources available to you. As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web (at least not until after I return exams to you). And, in case it's not clear, you may not ask others (in person, via email, or by posting a please help message) to put answers on the Web.

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date. It is likely to take you about five to ten hours, depending on how well you've learned topics and how fast you work. You should not spend more than ten hours on this exam. Stop at ten hours and write: There's more to life than CS and you will earn at least 80 points on this exam. I would also appreciate it if you would write down the amount of time each problem takes. I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they've spent at least seven hours on the exam or that they've finished the exam. (At that point, I may then change the exam.)

You must include both of the following statements on the cover sheet of the examination. Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. Note also that inappropriate assistance is assistance from (or to) anyone other than myself or our teaching assistant.

1. I have neither received nor given inappropriate assistance on this examination.
2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam. You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code.

You should fully document all of the primary procedures (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those, too, although you may opt to write less documentation. When appropriate, you should include short comments within your code. You should also take care to format your code carefully.

Just as you should be careful and precise when you write code, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by emphasizing your answer and including a clear set of work that you used to derive the answer.

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of classes this week and next to discuss any general questions you have on the exam.

## Problems

### Problem 1: Sorted Lists

Topics: Lists, Interfaces, Documentation

You may recall that I suggested that there are at least three basic kinds of lists:

• Simple Lists, which are expandable collections you can iterate.
• Ordered Lists, which are an extension of simple lists in which the client controls where in the list elements are added.
• Sorted Lists, which are an extension of simple lists in which a Sort Order determines the order in which the list elements are iterated.

As the definition suggests, sorted lists provide only the basic list operations; they just specialize those operations. How do you, as designer, indicate that the operations have been specialized? You write good documentation.

Design and document a `SortedList` interface and a `SortedListCursor` interface.

### Problem 2: Array-Based Stacks

Topics: Linear structures, Array-based implementations

Implement stacks using arrays. You need not document your stacks and you need not show me your testing (but you should test).

You need only implement the four central linear structure methods: `add`, `get`, `peek`, and `isEmpty`.

### Problem 3: Anti-Exceptions

Topics: Exceptions, Java Basics

In their attempts to better understand exceptions, Carla and Carl Caffeinated have decided to write a somewhat strange method that they call `antiExceptional`. Their method calls another method. If the called method throws an exception, their method increments a counter. If the called method does not throw an exception, their method throws an exception (and does not increment the counter).

Here's what they've come up with.

```    public static int exceptionCount = 0;
public static void antiExceptional(Object param)
throws Exception
{
try {
sampleMethod(param);
throw new Exception("sampleMethod succeeded");
}
catch (Exception e) {
++exceptionCount;
}
} // antiExceptional
```

Unfortunately, it seems as if their method never throws an exception. If `sampleMethod` fails, `antiExceptional` increments `exceptionCount` and returns normally, which is what they wanted. However, if `sampleMethod` succeeds, `antiExceptional` also increments `exceptionCount` and returns normally, which is not what they wanted.

Update the `antiExceptional` method so that it throws an exception when `sampleMethod` succeeds (and only then).

### Problem 4: Standard Object Methods and Linked Lists

Topics: Lists, Linked Structures, Java Basics

Most Java objects should provide four key methods:

• `String toString()`, which converts the object to a string;
• `boolean equals(Object other)`, which determines if the other object is naturally equal to the object;
• `Object clone()`, which creates a new copy of the current object; and
• `int hashCode()`, which generates a number for the object to be used in hashing (equal objects must have equal hash codes; unequal objects should have different hash codes, but may have equal hash codes).

Implement these methods for the `LinkedList` class.

Note that another object is equal to a `LinkedList` if it's a `List` and when you iterate the elements, they're the same.

Since we never completed the `LinkedList` class, you need not write working code. However, your code should convince me that you can write working code given sufficient time.

### Problem 5: Heaps

Topics: Linear structures, Trees, Sorting

Implement heapsort. You should build a `Heap` data structure, insert the values from the array to be sorted into the `Heap`, and then read them back out in order.

You need not document this code (although you should).

The only methods you need to include for your heap are the constructor, `add`, and `get`. You might also choose to implement `peek`.

## Errors

These are the errors observed by students. Since I have threatened to take off for grammatical or spelling errors, I give the whole class one point of extra credit for each such error they notice in this exam. Such extra credit is capped at five points.

• `sampleMethod(object)` instead of `sampleMethod(param)`. [EC, 1 point]
• `antiExceptional` is declared as `boolean` but is really of type `void`. [ON, 1 point]

## Questions and Answers

Here you may eventually find questions from your colleagues and my answers to those questions.

What should we do if the array fills in the array-based stack?
Throw an exception or expand the array. It's up to you.
What's a good strategy for your exams?
Start early. Ask questions. Focus on doing the problems that say "You need not write working code" first.
When will we go over hash code?
What else do you need to know? Friday.
What should `toString` return for a list?
Something that looks a lot like a Scheme list.
Can we recurse on the elements?
Certainly.
You say that a sorted list controls the order in which the elements are iterated. Doesn't it also control the order in which they are added and inserted?
Nope. Since the only way you see the elements of a sorted list are through iteration, the only important aspect to an outsider is iteration.
Since `add` can mess up iteration of sorted lists (e.g., if I add an element smaller than something I've already visited), can I note that `add` invalidates all cursors?
Certainly.
Didn't you promise us that you'd scale our successful work when we hit ten hours? All you've done is promise us a minimum grade.
I do not recall promising to scale your exams.
Do we cite sources if we simply take ideas and don't copy code or phrases directly?
Certainly. Such citation is a hallmark of academic honesty.
For problem 3, does the code have to work?
If all the other procedures are defined, your code should work.
What heap methods should we implement?
The constructor, `add`, and `get`.
How do I get my name printed on every page?
Save the exam in a file with your name on it (e.g., Rebelsy-Exam4). Print it with `a2ps`.
Do we really need to use the six P's everywhere?
You can use briefer documentation as long as it's clear that you've thought about all the details.

## History

Sunday, 27 April 2003 [Samuel A. Rebelsky]

• Created.

Monday, 28 April 2003 [Samuel A. Rebelsky]

• Finishing touches.
• Released.
• Almost immediately added questions and answers and corrections.

Wednesday, 30 April 2003 [Samuel A. Rebelsky]

• In a fit of insanity, updated exam to four out of five problems.

Saturday, 3 May 2003 [Samuel A. Rebelsky]

• Lots of questions and answers.
• Clarified what parts of the heap structure to implement.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:19:07 2003.
The source to the document was last modified on Tue May 6 08:12:57 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Exams/exam.04.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu