CS140 Midterm 2 Solutions

Spring 2020


Paper Portion

    1. map
    2. hash table
    3. vector
    4. deque
    5. multimap
    6. list
    7. hash table

    1. Lines 3 or 4.
    2. The code could seg fault on line 3 if the user name is not found and on line 4 if the prize is not found because the iterators would point to the sentinel node and the sentinel node will have a NULL pointer for "second". The fix is to move statements 3 and 4 after line 8, because if you get past line 8, then you know that the iterators point to legitimate nodes with valid User and Prize pointers. It is still okay to declare userPoints and prizePoints in their current position, but the assignment statements must be moved after line 8.
    1. Memory Leak
    2. You should remove the two new statements that unnecessarily allocate memory when x and y are declared:
          PersonNode *x;
          PersonNode *y;
      

      1. O(n3)
      2. O(n3)
      3. O(2n)
      4. O(1)
      5. O(n)
      6. O(log n)
      7. O(n log n)
      8. O(1.6n)

    1. Ranked from fastest to slowest
      1. T(n) = 10000000;
      2. T(n) = 500*log n;
      3. T(n) = 800n + 6000;
      4. T(n) = 16(n*log n) + n + 3
      5. T(n) = n3 + 20n2 + 1000;
      6. T(n) = 3n3 + 10n + 50;
      7. T(n) = 20*(1.6n)
      8. T(n) = 2n + 5*nlog n + 30;

    1. House(string streetAddr, string city, string state, int zip);
    2. House(const House &house);
    3. House(const House &house);
    4. House();
    5. House& operator= (const House &house);
    6. House();
    7. ~House();
    8. House(const House &house);
    9. ~House();

  1. I am showing the stack in ascending order but it was okay to show it in descending order:
    [mystery: line = 3, n = 2]
    [mystery: line = 4, n = 4]
    [mystery: line = 4, n = 8]
    [mystery: line = 5, n = 11]
    [mystery: line = 4, n = 22]
    [mystery: line = 5, n = 25]
    [mystery: line = 4, n = 50]
    [mystery: line = 5, n = 53]
    [main: line = 1]
    
    1. Base case: The string does not have a substring of the form "<>". In this case your diamond count is 0.
    2. Recursive case: Your diamond count is 1 + the number of diamonds returned by recursively processing the residual mine that is obtained by deleting the first substring of the form "<>" from the mine string.

    1. rabbit, hare, grizzly, bear, hound
    2. nit: hare
    3. nit1: sentinel
    4. rnit: grizzly
    5. rnit1: hound

Coding Portion

  1. void updateUser(list<User *> &userList, const string &name, double amount) {
      list<User *>::iterator lit;
      User *newUser;
    
      // iterate through the user list looking for the user. If we find the
      // user then update the user's balance. Since the list is sorted we can
      // break out of the loop as soon as the user's name is less than the name
      // of a node in the list because we then know that the user cannot be in
      // the list.
      for (lit = userList.begin(); lit != userList.end(); lit++) {
        if ((*lit)->name == name) {
          (*lit)->balance += amount;
          return;
        }
        else if (name < (*lit)->name)
          break;
      }
      // if we get here we did not find the user in the list--notice that whether
      // or not we reached the end of the list, lit points to the correct node.
      // If we reached the end of the list then lit points to .end() and it is
      // okay to insert before .end()
      newUser = new User(name, amount);
      userList.insert(lit, newUser);
    }
    	
  2. void Hotel::InsertReservation(string guest, int room, int checkInDate,
    				   int checkOutDate) {
      map<int, string> *roomOccupancy;
      map<int, map<int, string> *>::iterator reservationIter;
      map<int, string>::iterator roomIter;
      int date;
    
      // doess the room exist?
      reservationIter = reservations.find(room);
      if (reservationIter == reservations.end())
        throw (string) "no such room";
    
      // get the room's occupancy map and then get the first date on or after
      // the check in date when the room is occupied
      roomOccupancy = reservationIter->second;
      roomIter = roomOccupancy->lower_bound(checkInDate);
    
      // if the room is occupied on or after the check in date and before the
      // checkout date, then we cannot accommodate the reservation because someone
      // else is in the room for a portion of the reservation
      if (roomIter != roomOccupancy->end() && roomIter->first < checkOutDate) {
        throw (string) "room occupied";
      }
    
      // if we get here then we know that the room is unoccupied during the
      // requested dates and we should add the guest to the room's occupancy map
      // for each of the requested days
      for (date = checkInDate; date < checkOutDate; date++) {
        (*reservations[room])[date] = guest;
        // here's an alternative way to perform the assignment
        // roomOccupancy->insert(make_pair(date, guest));
      }
    }