CS140 Midterm 2

Fall 2018

Instructions

  1. Write your name clearly at the top of the exam
  2. You must answer all the questions on the exam. The exam has 8 problems. Make sure that you have all 8 problems.
  3. Write your answers on the exam pages and hand in the exam when you are finished.
  4. You have until the end of the class period to finish the exam. When time is called you must immediately drop your pen/pencil and hand in your exam.
  5. Ensure that you write clearly and legibly and that your diagrams are easy to read. Failure to do so may result in the deduction of points.
  6. The exam is closed note, closed book, closed neighbor.
  7. The total points available for this exam (lab coding questions plus this paper exam) is 150 points. There are 80 points on this exam and there are 70 points on the coding portion of the exam.
  8. Good luck!

  1. (12 points) Your boss has asked you to choose between two algorithms. The two algorithms have the following running times:
    	  Algorithm 1: T(n) = n + 5n2 + 80
    	  Algorithm 2: T(n) = 10000log n + 10000
            

    1. What is the Big-O running time of Algorithm 1?

    2. What is the Big-O running time of Algorithm 2?

    3. If the size of the input will be typically on the order of n < 50. Which of the two algorithms should you choose and why?
    4. If the size of the input will be typically on the order of n > 10000, which of the two algorithms should you choose and why?
      
      
          
  2. (14 points) For each of the following questions circle the best answer from the above list. Sometimes it may seem as though two or more choices would be equally good. In those cases think about the operations that the data structures support and what I said about them in class. Then choose the data structure whose operations are best suited for the problem. You may have to use the same answer for more than one question.
     
         a. vector
         b. list
         c. deque
         d. map
         e. set
         f. multimap
         g. multiset
         h. hash table
    

  3. (10 points) Suppose I have the following declarations and code:
         string a, b;
         string *x, *y;
    
    Also suppose that the above variables are assigned the following memory addresses:
         a: 0x1000
         b: 0x1100
         x: 0x1200
         y: 0x1204
     
    After each of the following code sequence executes, what are the values of a, b, x, and y? Assume that code segments 1 and 2 execute independently (i.e., code segment 2 does not execute after code segment 1.
           Code Segment 1
           x = &b;
           *x = "Smiley";
           y = x;
           *y = "Brad";
           b = *x;
    
           Code Segment 2
           x = new string("Michelle");    // address of new string = 0x2000
           a = *x;
           y = new string("Charles");     // address of new string = 0x2100
           x = y;
           b = *x;
    
           Code Segment 1                       Code Segment 2
    
           
           a: _______________________            a: _______________________
    
    
           b: _______________________            b: _______________________
    
    
           x: _______________________            x: _______________________
    
    
           y: _______________________            y: _______________________
    
    
                                                *x: _______________________
    
    
                                                *y: _______________________
    
    
  4. (6 points) Suppose you are given the following code:
    class Picture {
    public:  
        ifstream *sourceFile;
        int rows;
        int cols;
    };
    
    Picture *p;
    string *name1, *name2;
    
    name1 = new string("Hank Aaron");
    name2 = name1;
    delete name1;
    p = new Picture();
    p->sourceFile = new ifstream();
    p->rows = 30;
    p->cols = 40;
    *name2 = "Hammerin Hank Aaron";
    p->sourceFile->open("mountain.jpg");
    

    Answer the following questions about the above code:

    1. __________________________________ This term is used to describe what name2 becomes after name1 is deleted.

    2. What is likely to happen when p->sourceFile->open is executed and why is it likely to happen? Use no more than 3 sentences for your answer.
      
      
      
      
      
      
      
      
      
      
      
      
      
      	  
  5. (8 points Suppose I want to insert n telephone numbers into the following data structures and then perform n finds, one for each of the telephone numbers. For each of the following three data structures give the total Big-O running time for the find and insert operations (i.e., I do not want the running time for a single insert operation but for all n insert operations). Make the following assumptions:

    Data StructureInsertFind
    Map  
    Hash Table  
    List  
    Vector  

  6. (10 points) The following code is reading the first and last names of individuals and is keeping track of all of the first names that are associated with a last name. For example, if the input is:
          Brad VanderZanden
          Smiley VanderZanden
          Minnie Mouse
          Mickey Mouse
          Ebby VanderZanden
        
    then the set {Brad, Ebby, Smiley} will be associated with "VanderZanden" and the set {Mickey, Minnie} will be associated with "Mouse".
      typedef set <string> fnset;
    
      map <string, fnset> lnames;
      string fn, ln;
      fnset firstnames;
    
      while (cin >> fn >> ln) {
         firstnames = lnames[ln];
         firstnames.insert(fn);
         lnames[ln] = firstnames;
      }
        
    1. Explain why the following code is inefficient (it computes the correct result but is inefficient). Use no more than 3 sentences to describe the problem.
      
      
      
      
      
      
      
      
      
        
      
      
      
      
    2. How could you rewrite the code to fix the inefficiency. Only modify that portion of the code that needs to be changed.
      
      
      
      
      
      
      
      	

  7. (12 points) Assume you have the following declaration:
    class ListNode {
       public:
          string name;
          ListNode *next;
          ListNode *prev;
    
          ListNode(string n) : name(n) {}
    };
    
    Further suppose that a series of inserts have created the following list:

    The code below is supposed to move the node containing "Ben" so that it is between "Nancy" and "Sarah". However, the code eventually seg faults and there are some other issues with the code as well.

    1)      ListNode *nextNode = currentNode->next;
    2)      currentNode->prev = currentNode->prev->prev;
    3)      currentNode->next = currentNode->prev;
    4)      nextNode->prev = currentNode->prev;
    5)      currentNode->prev->prev = currentNode;
    6)      currentNode->next->next = nextNode;
          
    1. Draw the diagram that results when the code above is executed up until the statement that seg faults (include nextNode in the diagram). Do not include the seg faulted statement or the effects of executing any statement after the seg faulted statement.
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
           
    2. Which statement seg faults? ________________________

    3. What does the seg faulting statement appear to be trying to accomplish? Please say something like "it was trying to make Sarah's next field point to Ben's node".
      
      
      
      
      
      
      
      
      
      	 
    4. One reason the above code is buggy is because it tries to avoid using temporary variables by traversing multiple pointers in a single expression (e.g., current->next->next). These types of multiple traversals often result in buggy code. Re-write the above code so that 1) it successfully places the node containing "Ben" between the nodes containing "Nancy" and "Sarah", and 2) it is easier to read by introducing an additional temporary variable and eliminating multiple pointer traversals (i.e., in any pointer expression, there should be only one ->).
      
      
      
      
      
      
      
      
      
      
      
      
      
      
            
  8. (10 points) Assume you have the following code:
    class Person {
    public:
      string name;
      Person *bestFriend;
    
      Person(string n) { name = n; }
    };
    
    string names[] = { "mickey", "bugs", "daffy", "winnie" };
    list<Person *> friends;
    Person *newPerson;
    Person *lastPerson;
    int i;
    
    lastPerson = NULL;
    
    for (i = 0; i < 4; i++) {
        newPerson = new Person(names[i]);
        newPerson->bestFriend = lastPerson;
        friends.push_back(newPerson);
        lastPerson = newPerson;
    }
    
    Draw a diagram that shows the list, the Person objects that get created, and the objects to which each pointer points when the code has finished execution. Make sure that: