Review Questions for Midterm 
CS409 - Spring 1999
The exam will take place at 8:40am (our regular time) in Upson 205 (our regular
classroom) on Thursday, March 11.  I will try to arrive early so that the chairs can
be spread out somewhat and so that we can start right at 8:40.
The use of extra material is not permitted during the exam (i.e., no textbook, no
notes, no calculator, etc.). 
The exam includes material covered in lecture through March 9 and the corresponding
material in the text (Chapters 1, 2, 4.1, 4.3, 7.1-3, 7.5, 11, 12.1-3, 13.1-3, 14.1-2, 16,
19.1-2, 22.1-3, 31.2, 35.4).  See the online schedule for
a list of topics covered.
The exam will attempt to gauge your knowledge and understanding of course concepts. You
should understand the terms used in the course and understand how the various data
structures and algorithms work, but you shouldn't need to memorize the specific code given
in the text or in lecture. 
The review questions given here are not meant to exhaust the possible topics for exam
questions.  Check the online schedule and consider
reviewing the homework assignments and quizzes. 
There will be no new homework assigned this week. 
Review Questions 
  - Fill in the table below with the expected time for each operation. Use big-O
    notation. The operations are insert (place a new item in the data structure), member (test
    if a given item is in the data structure), getMin (return the value of the minimum item in
    the data structure and delete it from the data structure), and successor (given an item,
    return the successor of that item). 
      
        | Data Structure | insert | member | getMin | successor |  
        | sorted array |  |  |  |  |  
        | unsorted array |  |  |  |  |  
        | balanced tree (red-black tree) |  |  |  |  |  
        | hashing |  |  |  |  |  
        | sorted linked list |  |  |  |  |  
        | unsorted linked list |  |  |  |  |  
 
- Short Answer. 
      - Where is the smallest element in a red-black tree?  In a 234-tree? 
- When the subject is balanced trees, what does rotation mean? 
- What is path compression in the union/find algorithm? 
- How long does it take to insert a new element into a heap? To return the smallest thing
        in a min-heap? To delete the smallest thing in a min-heap? To find the largest thing in a
        min-heap? 
 
 
- The following picture represents a 234-tree.                   24
               /      \
        2 , 22            30 , 40 , 48
      /   |     \      /    /      \      \
     1  5,11,19  23  29  32,36  41,42,43  50
      - Draw an equivalent red-black-tree. 
- Draw a picture of the 234-tree that results from inserting 31 into the original 234-tree.
      
 
- For each of the following problems, choose the best of the listed data structures and
    explain why your choice is best. Where several operations are listed, you should assume,
    unless stated otherwise that the operations occur with about equal frequency. 
      - Operations are Insert, DeleteMax, and DeleteMin. 
 balanced tree or sorted doubly-linked list
- Operations are Insert and FindMedian. (The median is the item m such that half the items
        are less than m and half are greater than m.) 
 red-black trees or sorted array
- You have a dictionary containing the keywords of the Pascal programming language. 
 ordered array or red-black tree
- You have a dictionary that can contain anywhere from 100 to 10,000 words. 
 unordered linked-list or red-black tree
- You have a large set of integers with operations insert, findMax, and deleteMax. 
 unordered array or hashing with linear probing
 
- You have a hashtable of size m=11 and a (not very good) hash function h:
 
 h(x) = (sum of the values of the first and last letters of x) mod m
 
 where the value of a letter is its position in the alphabet (e.g., value(a)=1, value(b)=2,
    etc.). Here are some precomputed hash values:
      
        | word | ape | bat | bird | carp | dog | hare | ibex | mud | koala | stork |  
        | h | 6 | 0 | 6 | 7 | 0 | 2 | 0 | 6 | 1 | 8 |  
 
Draw a picture of the resulting hashtable (using chaining) after
    inserting, in order, the following words: ibex, hare, ape, bat, koala, mud, dog, carp,
    stork. Which cells are looked at when trying to find bird.  
- Suppose you are given the following information about a hashtable. 
      
        | Space Available (in words) | 10000 |  
        | Words per Item | 7 |  
        | Words per Link | 1 |  
        | Number of Items | 1000 |  
        | Proportion Successful Searches | 1/3 |  
 
What is the expected number of probes for a search operation when
    hashing with chaining is used? 
- Consider a tree implementation for the union/find problem in which the smaller set is
    merged to the larger and the name of the set is taken to be the element stored at the root
    of the tree. Suppose we initialize our sets so that each integer between 1 and 8
    (inclusve) is contained within its own set. 
      - Give a sequence of seven unions that produces a tree whose height is as large as
        possible. Your answer should be a sequence of procedure calls of the form Union(a,b)
        where a and b are integers between 1 and 8. Draw the resulting tree. 
- Give a sequence of seven unions, on the original eight sets, that produces a tree of
        minimum height. Draw the resulting tree. 
- Explain why both the min- and max-height trees use seven unions.
 
 
- The following questions refer to an implementation of an ADT with operation Insert,
    Delete, and isMember. 
      - Under what conditions would you use a red-black tree instead of hashing with chaining? 
- Under what conditions would you use an unordered array instead of a red-black tree? 
- Under what conditions would you use a binary search tree instead of a heap? 
- What implementation would you use to get the best expected time for a search? 
- What implementation would you use to get the best worst-case time for a search? 
 
- Consider the following program outline where |A| represents the number of items in array
    A.   method Review (array A) {
     if (|A| > 1) {
        Do something to A that takes time O(|A| log(|A|));
        Split A into two equal size pieces called B and C;
        Review(B); Review(C);
        Modify A using O(|A|) time;
     }
  }What is the recurrence that describes the time used by this program?  
- 
      - 
        | 234 Tree (Dictionary) | Max-Heap (Priority Queue) |  - 
        |       (3 , 5)
     /   |   \
(1,2)   (4)   (6) |      6
   /   \
  3     4
 / \
1   2 |  
 - For each of the preceding trees, find a sequence of appropriate operations that will
    produce it starting from an empty tree.  
- Determine the best possible big-O bounds on T(n) where T(n) is defined by the following
    recurrence:
 T(n) = cT(n/2) + n2 and T(1) = 1.  The variable c here represents a
    positive constant.  You should have 3 different answers depending on the value of c.
- You are given a k×k checkerboard with a nonnegative number in each square (the square
    colors are not significant).  A token is moved from square to square on the board.
      Each time the token enters a square it is charged the amount written in that
    square.  Assume that the only legal moves are to-the-right, down, and diagonally
    right-down.  Give an algorithm that runs in O(k2) time to find the cost of
    the minimum cost sequence of moves beginning in the upper left corner of the board and
    ending at the lower right corner.
- Suppose we are given a sequence A = (a1,a2,...,an) of
    numbers.  Describe an O(n2) time algorithm for finding the longest
    subsequence of A such that the numbers in this subsequence are monotonically increasing.
      (Hint: For each ai, compute the longest increasing subsequence that ends
    at ai.)  It's possible, but more difficult, to develop an algorithm that
    takes time O(n log n). [from Goodrich & Tamassia 98]