[an error occurred while processing this directive]
Final Exam Out: 1 May 2006
Due: Monday, 8 May, 4:59PM

Name: _________________________________________________________

Scores
123456789Total
          
1051010101010101085

Directions

Work alone. You may not discuss these problems or anything related to the material covered by this exam with anyone except for the course staff between receiving this exam and class Monday.

Closed web. You may not search the web to attempt to find answers to the questions on this exam. You may you web pages linked from the CS216 web site, but may not do web searches to attempt to find specific answers.

Open other resources. You may use any books you want, lecture notes and slides, your notes, and problem sets. If you use anything other than the course books and notes, cite what you used. You may not use other people.

Open tools. You may run any program you want, including a Python interpreter, C compiler, Java compilers, Java VM, and x86 assembler for this exam. You are not expected to need to do this, and will not lose points for minor syntactic mistakes.

Answer well. Write your answers on this exam. You should not need more space than is provided to write good answers, but if you want more space you may attach extra sheets. If you do, make sure the answers are clearly marked.

Answer all 9 questions. The questions are not necessarily in order of increasing difficulty, so if you get stuck on one question you should continue on to the next question. There is no time limit on this exam, but it should not take a well-prepared student more than a few hours to complete.

Full credit depends on the clarity and elegance of your answer, not just correctness. Your answers should be as short and simple as possible, but not simpler.

Algorithm Analysis

1. (10) What is the asymptotic running time for the findMedian procedure defined below? You should be careful to state all assumptions you need for your answer to hold, to define the meaning for all variables you use in your answers. If the worst case and expected case aymptotic running times are different, you should describe them both; if they are the same, you should explain why.
# pre: requires arr is a list of numbers containing at least one element
# post: returns the median value in arr
# modifies: arr
def findMedian (arr):
   assert (len(arr) > 0)
   
   if (len (arr) == 2):
       return (arr[0] + arr[1]) / 2
   
   maxind = 0
   maxval = arr[0]

   for i in range (1, len (arr)):
       if arr[i] > maxval:
           maxind = i
           maxval = arr[i]

   minind = 0
   minval = arr[0]
   for i in range (1, len (arr)):
       if arr[i] < minval:
           minind = i
           minval = arr[i]

   if minind == maxind:
       return maxval
   else:
       # remove min and max from list
       del arr[minind]
       if maxind > minind: maxind = maxind - 1
       del arr[maxind]
       return findMedian (arr)

2. (5) What is the asymptotic memory usage of findMedian?

3. (10) What is the asymptotic running time for the htree_print procedure (from Problem Set 4) defined below? As with Question 1, be careful to state all assumptions you need for your answer to hold, to define the meaning for all variables you use in your answers. If the worst case and expected case aymptotic running times are different, you should describe them both; if they are the same, you should explain why.
static void htree_print_indent (htree orig, htree t, int indent) {
  if (t == NULL) {
    return;
  } else {
    int i;
    for (i = 0; i < indent; i++) {
      printf (" ");
    }
    if (t->letter > 0) {
      printf ("%d [%c] (%d) => %s\n", (int) t->letter, t->letter,
	      t->count, htree_encodeChar (orig, t->letter));
    } else {
      printf ("%d (%d)\n", (int) t->letter, t->count);
    }

    htree_print_indent (orig, t->left, indent + 2);
    htree_print_indent (orig, t->right, indent + 2);
  }
}

void htree_print (htree t) {
  htree_print_indent (t, t, 0);
}

Exams Redux

4. (10) The solution code for Question 9 on Exam 1 has a serious bug in it. Explain the bug and describe a fix.
def allPossiblePartnerAssignments (students):
   s = students[:] # we use a copy to avoid modifying input list
   for n in range (len (students)):
      s.append (None)
   for ordering in allLists (s):
      assignments = { }
      for i in range (len (ordering)):
         if i + 1 == len(ordering):
            assignments[ordering[i]] = None
         else:
            if not ordering[i] == None: # avoid adding partners for None
               assignments[ordering[i]] = ordering[i + 1]
            if not ordering[i + 1] == None:
               assignments[ordering[i + 1]] = ordering[i]
      yield assignments      

5. (10) Question 4 of Exam 2 established that simple garbage collection strategies will not work for C programs. Construct a C program fragment where a standard mark-and-sweep garbage collector would incorrectly garbage collect a live object. Your answer should identify the point in the program where the garbage collector runs, and explain why it would mistakenly identify a non-garbage object as garbage.

Assembly Programming

6. Shown below are two instruction sequences. Add or remove as few instructions as possible to make the second instruction sequence have equivalent behavior to the first sequence. You should assume that the called function _func takes two parameters and follows the C calling convention, but make no other assumptions about it. Show your answer by clearly marking the needed changes to the second sequence.

First sequence:

   xor edx,eax
   imul edx, [ebx]
   push ecx
   push [ebx]
   push 2
   call _func
   add esp, 8
   pop ecx 
Second sequence (modify this to show your answer):





   push [ebx]




   push 2




   call _func




   pop esi




   pop ecx


7. Explain two reasons why the C calling convention makes the caller responsible for removing the parameters from the stack, instead of making the callee responsible for this.

Hash Tables

8. Consider a hash table with sequential open addressing, a hash function that is
H(key) = (key[len(key) - 1] - 'A') mod m
(that is, the last letter in the key determines the index, where 'A' is index 0, and 'Z' is index 25 mod m). You may safely assume all keys end with a captial letter ('A' - 'Z').

Does the order in which keys are inserted into the hash table matter? Either prove that for any set of keys (less than m) the total number of table lookups needed to insert all the keys will always be the same, or show that the order in which keys are inserted affects the total number of table lookups needed.

Sarge's Revenge

9. (10) Last year, North Carolina passed a law ( S223: Public Confidence in Elections) that (among other things) required that vendors of voting machines provide their source code to the state for review. How much will source code review help ensure the voting equipment is correct and fair? Describe additional requirements that would be necessary to make this useful.


















10. (optinal, no credit) Is there anything else I should know to fairly assign your final grade for this course? (use back or attached sheet for more space)



End of Exam. Enjoy your summer!
[an error occurred while processing this directive]