CS140 Midterm Exam - March 12, 2013 Answers and Grading


Question 1: 12 Points

Answers Summarized

Separate Chaining Linear Probing Quadratic Probing
Scenario #1 88 89 89
Scenario #2 99 2 8
Scenario #3 50 63 None

Grading

1 point for the separate chaining answers, 1.5 for the others. Leaving Scenario 3 blank did not constitute a correct answer -- you needed to state that an empty entry cannot be found.


Question 2: 10 Points

I'm going to be honest -- I was very disappointed with the majority of the answers. The scores here were worse than question 6. Obviously, when so many students do poorly on a question, it's an indication that I am not teaching the material correctly, or emphasizing the points that I am testing enough. I will work on that.

There are a few points here, though, that I want to stress, and if you are studying from this exam, please take this to heart:

Answers

Part A: You want a procedure that either inserts into the front of the vector or deletes from the front of the vector. The reason is that with a deque, those are constant time operations, and with a vector, those are linear operations. We cover this in the lecture entitled "Lists, Iterators, Bad Vector Usage, Deques." Here's an example (in q2.cpp):

void X(vector <double> &a)
{
  int i;

  while (!a.empty()) a.erase(a.begin());
}

If a has n elements, then this procedure performs n(n+1)/2 operations with a vector, and only n with a deque.

Many of you tried to insert into arbitrary places in the vector. Unfortunately, that will work just as poorly with a deque. It's only in the beginning when the deque matters.

Part B: Lists are useful because you may insert in front of any element, and it is guaranteed to run in constant time. Similarly, you may delete any element and it is guaranteed to run in constant time. So, a list is advantageous over a deque when you are inserting to or deleting from the middle of the list, rather than the first or the last element. An example is the "DiamondHunt" example from the "Lists, Iterators, Bad Vector Usage, Deques" lecture.

Grading


Question 3: 12 Points

Grading

Three points per answer. On part A, you received 1 point for 0x2e and 0x30. On Part C, you received 1 point for 608, 609, 60b, 60c, 60d, 60e, 60f, 50a, 40a, 70a, 80a, c0a and 600. I gave half credit if you bit-shifted the wrong way and answered 0x65 in part D. I also gave half credit for 96 (left shift on a byte), and 97 (circular left shift on a byte).


Question 4: 12 Points

This program is in sid.cpp. Please feel free to compile it and do a cut-and-paste on the input. Key points are that every word is read, regardless of whether there are multiple words on a line, and that the "10" and "Mario" are ignored because they are on the command line:

Grading

I asked yes/no questions here and assigned points for the answers:


Question 5: 10 Points

This is a nuts and bolts topcoder question (SRM 337, D2, 250-pointer). You need to compare indices i and s.size()-i-1, and set both to the smaller character. Here it is with a main() for testing: s_to_p.cpp

#include <iostream>
using namespace std;

string s_to_p(string s)
{
  int i, j;

  i = 0;
  j = s.size()-1;
  while (i < j) {
    if (s[i] < s[j]) {
      s[j] = s[i];
    } else {
      s[i] = s[j];
    }
    i++;
    j--;
  }
  return s;
}

main()
{
  string s;

  while (cin >> s) cout << s_to_p(s) << endl;
}

Grading

10 points. Grading these is objective -- I look over your program and assign points on how well I think you understood the problem and coded your answer.


Question 6: 12 Points

The strategy here was to look at the difference between the header files: Once you see the difference, you look over a program or two, and see that there are only a few places where they differ. The strategy is to think about each of those differences and how they may relate to compiler errors and printing out wrong values. That lets you assign 4's, 5's and 3's to various header/implementation combinations: If you want corroboration, q6-script.sh is a shell script that compiles the header/implementation combos and tests:
UNIX> sh q6-script.sh 1 A
Header 1.  Implementation A

Compilation failed:

q6-test.cpp: In function 'void print_LVec(LVec&)':
q6-test.cpp:14: error: cannot convert 'std::list >' to 'L*' in assignment
UNIX> sh q6-script.sh 1 B
Header 1.  Implementation B

Compilation succeeded -- running

10
10.01
10.02
10.03
10.04
10.05
10.06
10.07
10.08
10.09
10.1
10.11
10.12
10.13
10.14
UNIX> 
All of the combinations are in q6-outputs.txt.

Grading

0.67 or 0.66 points per answer. Partial credit -- you got 0.25 points for answers that are in the parentheses below:

Out of curiosity, I wrote a program to simulate 10 million students answering randomly:

Clearly, y'all did much better than that -- yay!! Your average was 5.57, while that average was 3.02.