This preview shows pages 1–4. Sign up to view the full content.
1.00/1.001 Introduction to Computers and Engineering Problem
Solving
Final Examination  December 15, 2003
Name:
TA:
Section:
Email Address:
You have 3 hours to complete this exam. For coding questions,
you do not need to include comments, and you should assume
that all necessary files have already been imported.
Good luck!
Question
Points
Question 1
/ 10
Question 2
/ 10
Question 3
/ 20
Question 4
/ 60
Total
/ 100
1 / 15
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Question 1. True or False (10 points)
Answer the following questions about Java by circling TRUE
or FALSE as appropriate.
1)
If
a
and
b
are two references of the same type, then
a.
equals(
b)
will return true if
and only if
a
and
b
refer to the same object.
TRUE
FALSE
2)
Collisions will never occur in a hash table that uses chaining.
TRUE
FALSE
3)
Java streams are Last In, First Out stacks.
TRUE
FALSE
4)
It is not possible to read and write one file using the same stream.
TRUE
FALSE
5)
An inorder traversal will visit the nodes in a binary search tree in increasing order.
TRUE
FALSE
2 / 15
There are situations or specific sets of data that can make an efficient algorithm or data
structure give atypically inefficient or incorrect performance. Please describe at least two
examples and strategies to remedy the problem in both cases.
Example 1:
A standard binary search tree will degenerate into a linked list if a sorted set of keys
is inserted in order. This means that the tree’s normal insertion and retrieval time
of O(log n) will worsen to that of a linked list, O(n). The solution us ed by most tree
data structures in production algorithms is to rebalance the tree after certain
operations if it starts to become unbalanced.
Example 2:
If simple quicksort uses the first or last element of the current array as the pivot and
the sort is run on already sorted data, then one of the resulting subarrays will
always contain only one element, and the efficiency of the sort will decrease from
O(n lg n ) to O( n^2 ).
Perversely, insertion sort, which usually runs in O( n^2 ), will run in O( n ) on
presorted data, so insertion sort will actually run faster than quicksort in this case.
The best approach to solving the problem is to choose the pivot more carefully. If
the pivot is chosen as the median value of the first, last, and middle value of the
array to be sorted, quicksort will maintain its O( n log n ) performance on all
normal data.
Example 3:
If a hash map inserts keys which turn out to hash to the same slot or small set of
slots, the performance on insertions and retrievals can decrease to O( n ) like a
linked list rather than the normal O( 1 ). The solution is to use a better hash code
method in the key class or if there is some periodicity in the key’s hash code that
matches the number of cells in the hash map, to use a more sophisticated mapping
of hash codes than taking the modulus. Making the hash function rehash the integer
hash codes may help.
3 / 15
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.
 Spring '05
 GeorgeKocur

Click to edit the document details