CS302 -- Midterm Exam. March 13, 2008

Please, write your answers on a separate sheet, not on the exam.

Remember your name, too...

Question 1

The following is an array representation of a priority queue.

Part 1: Assume that the queue is as pictured above. Draw the array representation of the priority queue when the maximum value is removed from the queue.

Part 2: Assume that the queue is as pictured above. Draw the array representation of the priority queue when the value 66 is inserted into the queue.

Question 2

Use the following multiple choice answers to this question:

a. O(1) b. O(log2(n)) c. O(n) d. O(n log2(n)) e. O(n2)) f. O(n3))

For each activity, what is the running time complexity of the activity.

Question 3

Suppose you are provided with the function merge() with the following prototype:

void merge(double *a1, int size1, double *a2, int size2, double *a3);

merge() assumes that a1 is an array of size1 doubles, that a2 is an array of size2 doubles, and that a3 is an array of size1+size2 doubles. It also assumes that a1 and a2 are sorted. When it completes, a3 will contain all the values of a1 and a2 in sorted order.

Write the procedure merge_sort(), with the following prototype:

void merge_sort(double *a, int size);

You may write a helper procedure or two if you need it.

Obviously, merge_sort() should use merge() to sort the size doubles in array a using the Merge Sort algorithm. Make sure that your procedure does not have a memory leak.

Question 4

Behold the following undirected graph:

Which of the following are valid pre-order printings of the nodes given a depth-first traversal starting with the node Set. Answer all that are valid (there may be more than one).

A: Set You Free Why Babe Me Don't D: Set Me Free Why Don't You Babe
B: Set You Me Don't Free Babe Why E: Set You Don't Free Babe Why Me
C: Set Me Why Babe You Don't Free F: Set Me Why Babe Free Don't You

Extra Credit: What's the name of the song and who sang it?

Question 5

#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;

class LNames {
    LNames(string ln, string init_fn);
    string lname;
    int n;
    set <string> fns;

typedef map <string, LNames *> LNMap;

LNames::LNames(string ln, string init_fn)
  lname = ln;
  n = 1;
  cout << "Created " << ln << 
       " with " << init_fn << endl;
  LNMap lnames;
  LNames *l;
  string ln, fn;
  LNMap::iterator lnit;

  while (!cin.fail()) {
    cin >> fn;
    if (!cin.fail()) {
      cin >> ln;
      lnit = lnames.find(ln);
      if (lnit == lnames.end()) {
        l = new LNames(ln, fn);
        lnames.insert(make_pair(ln, l));
      } else {
        l = lnit->second;
        cout << "Added " << fn << 
             " to " << ln << endl;
  // HERE

What is the output of the above program when the following is given as standard input:

James Woods
Ickey Woods
Woody Austin
Tiger Woods
Woody Woodpecker
Steve Austin
Woody Herman

Question 6

Suppose in the program above, the line "// Here" was replaced with:
Write print_all_names(), which should print all the names, one per line, in the format:
last, first
The output should be sorted by last name, and if two people share the same last name, then they should be printed in order of first name.

Question 7

Suppose the cumulative distribution function of a probability distribution is:

CDF(x) = (x2)/9

for 0 ≤ x ≤ 3.

Answer the following questions:

Part A: Why do we restrict x to be ≤ 3?

Part B: In English, tell me what it means that CDF(1) = 1/9.

Part C: When we choose a random number according to this probability distribution, which is more likely:

Part D: Which of the following will set a to be a random number according to the probability distribution given by the above CDF. Assume that all variables used are doubles.

a.{ a = drand48() / sqrt(3.0); }
b.{ a = drand48() / 3.0; }
c.{ a = drand48() / 9.0; }
d.{ a = drand48() * sqrt(3.0); }
e.{ a = drand48() * 3.0; }
f.{ a = drand48() * 9.0; }
g.{ d = drand48(); a = d*d / sqrt(3.0); }
h.{ d = drand48(); a = d*d / 3.0; }
i.{ d = drand48(); a = d*d / 9.0; }
j.{ d = drand48(); a = d*d * sqrt(3.0); }
k.{ d = drand48(); a = d*d * 3.0; }
l.{ d = drand48(); a = d*d * 9.0; }
m.{ a = sqrt(drand48()) / sqrt(3.0); }
n.{ a = sqrt(drand48()) / 3.0; }
o.{ a = sqrt(drand48()) / 9.0; }
p.{ a = sqrt(drand48()) * sqrt(3.0); }
q.{ a = sqrt(drand48()) * 3.0; }
r.{ a = sqrt(drand48()) * 9.0; }