1. Tear off this sheet and use it to keep your answers covered at all times. If you need more scratch paper, I have some.
  2. Write your name on the back of the test by the staple to facilitate returning the exams. Do not write your name any other place.
  3. If you don't understand a question, come and ask me about it!
  4. Do NOT use ink. Programming often requires one to change his/her mind.
  5. No question is meant to have a syntax error. If it does, fix it or ask me how I want the error resolved.
  6. You are graded not only on the correct answer, but also the best answer.



1 CS 2200 - Exam # 1
Fall 2000 100 Points

Multiple Choice (3 points each)

  1. For the timing information below, what is the complexity?


    tabular34

    (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(tex2html_wrap_inline98)

  2. For the timing information below, what is the complexity?


    tabular37

    (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(tex2html_wrap_inline98)

  3. Algorithm C has complexity O(n). What do you expect to happen to the execution time if the problem size doubles?
    1. doubles
    2. slightly more than doubles
    3. quadruples
    4. increases by a constant
  4. Algorithm D has complexity O(tex2html_wrap_inline98). What do you expect to happen to the execution time if the problem size doubles?
    1. stays the same
    2. doubles
    3. slightly more than doubles
    4. quadruples
    5. increases by a constant
  5. In hashing, the term primary clustering means that
    1. the hash function is repeatable
    2. values that hash to the same orginal location do not keep competing in subsequent probes.
    3. values that hash to different orginal locations do not keep competing in subsequent probes.
    4. none of the above
  6. In hashing, what is the advantage of quadratic probing
    1. primary clustering is eliminated
    2. secondary clustering is eliminated
    3. rehashing is rarely necessary
    4. none of the above
  7. You have two solutions for the same problem. One is O(tex2html_wrap_inline98) and the other is O(n log n). When you compare the times for various problems, the O(n log n) algorithm is always significantly slower. How do you explain this?
    1. The clock is not accurate.
    2. Your problem sizes are too small to see the advantages of the O(n log n) algorithm.
    3. You are running on a very fast machine.
    4. The O(tex2html_wrap_inline98) algorithm has a higher overhead.
  8. When an exception is thrown, what is true?
    1. The program will cease execution.
    2. After the catch statement block is executed, you resume execution following the catch block.
    3. The catch statement has no access to information about the exception.
    4. A catch block cannot throw another exception.
    5. All of the above.
  9. When a hash function exhibits primarily clustering, we are concerned because
    1. You may fail to find items which are present
    2. The time to find a node can exceed O(log n)
    3. The hash function becomes non-repeatable.
    4. Rehashing will not eliminate the clustering.
  10. You have a hash table of size N with N/2 elements in the table. In trying to place a new key, a hash function claims it has made N probes yet it has not found a free location. Which best explains how this could happen?
    1. You have primary clustering
    2. You have secondary clustering
    3. every unused position is ``Deleted''
    4. the collision resolution algorithm is in a cyclic state of retrying the same few locations repeatedly
  11. Consider the following double hashing algorithm in which each key has a personalized step function as defined below:
    step(key) = 1 + (key mod 8)
    tex2html_wrap_inline112 = key mod 10

    What are the series of locations that will be tried for key = 5 (assuming that every probe results in a collision)?

    1. 5,1,7,3,9,5
    2. 5,9,3,7,1,5
    3. 5,8,1,4,7,0,3,6,9,2,5
    4. 5,6,7,8,9,0,1,2,3,4,5
    5. none of the above
  12. From our theorem we know:
    T(n)= a T(n/b) + O(tex2html_wrap_inline116)
    if tex2html_wrap_inline118 T(n) is O(tex2html_wrap_inline120)
    if tex2html_wrap_inline122 T(n) is O(tex2html_wrap_inline124)
    if tex2html_wrap_inline126 T(n) is O(tex2html_wrap_inline116)

    Consider the following algorithm:

    int doit(int A[], int n){
        if (n <=1) return 1;
        int t;
        for (int i =0; i < n; i++)
           t++;
        t+= 3*doit(A,n/2)
        for (int i =0; i < n; i++)
           t++;
        return t;
    }
    What is the complexity?
    (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(tex2html_wrap_inline98)
  13. For the problem above, what are the values of a, b and k?
    1. a=1, b=1, k=1
    2. a=1, b=2, k=1
    3. a=1, b=2, k=0
    4. a=3, b=2, k=2
  14. Why is lazy deletion used to delete from a hash table?
    1. to ensure efficient insertion
    2. to prevent the search routine from assuming a present item is deleted
    3. to reduce the load factor
    4. all of the above
  15. Consider the following code on a binary tree. What is the result of the call doit(Tree1)?

    tex2html_wrap132

  16. For the code below, what is the result of a call to also(Tree2)?

    tex2html_wrap134

    (a) 10 (b) 12 (c) 62 (d) 37 (e) none of the above

  17. For the code below, what is the result of a call to compute(Tree3)?

    tex2html_wrap136

    (a) 3 (b) 5 (c) 0 (d) 1 (e) none of the above

Short Answer

  1. (15 points ) Find the complexity of the following pieces of code. For each problem, draw an appropriate picture to justify your answer.
    1. void doit(int n)
      { if (n/2 <=1) return;
        int x = 0;
        for (int i = 0; i < n; i++) x++;
        doit(n/2);
      }
    2. void doit(int n)
      { if (n/2 <=1) return;
        doit(n/2);
        doit(n/2);
      }

    3. void doit(int n)
      { if (n <=0) return;
        for (int i=0;i<n;i++)
          for (int j=0;j<i;j++)
              cout << i*j;
      }

  2. (20 points) Given a binary tree, write the function which returns TRUE if every node in a tree is bigger in value than it's children.

    For example, for the following trees

    isBig(tree1) = true;
    isBig(tree2) = false;

    tex2html_wrap138

  3. (14 points) Answer the following questions about the code below.

    void swap(int a[], int i, int j)
    {  int temp= a[i];
    
       a[i] = a[j];
    
       a[j]=a[i];
    }
    
    void sort(int a[], int size)
    {
     int big = size;
    
     for (int i=0; i <size; i++)
    
         if (a[big] < a[i]) big = i;
    
     swap(a,size,big);
    
     sort(a,size-1);
    
    }

    1. What type of sort is coded?
    2. Correct any errors.
    3. What does size represent?
    4. Is the sort stable? Explain.
    5. Is the sort oblivious? Explain.


Vicki Allan
Sat Jan 27 09:49:54 MST 2001