CS 1501 Spring 2001
Practice Questions for Midterm Exam
Here are some example questions that may help
you to study for the midterm exam. Try to answer the questions fully before
looking at the answers. Though these questions indicate some of the material
that may be on the exam, they are by no means comprehensive. Remember to also
study EVERYTHING up to the last class before the exam on the Syllabus and Class
Schedule and the Algorithms Covered links, as well as everything pertaining to the first
3 assignments.
Fill in the Blanks Complete the statements below with the MOST APPROPRIATE words/phrases.
a) Two variations of QuickSort designed to minimize the probability of the worst case are __________________ and ___________________.
b) The GradeSchool integer multiplication algorithm requires Theta______________ time for N-bit integers.
c) If I have an open addressing hash table of size M, and a cluster of size C, the probability that a random key will be Inserted into the location immediately after the cluster is _________________.
d) If I am using an RSA encryption scheme, I may DECRYPT my message first and have a receiver later ENCRYPT it for the purpose of having a ______________________________.
Short Answers and Calculations
1) You have two programs, Program A, which runs in time k1N and Program B, which runs in time k2log2(N) for some constants k1 and k2. Assume that for a problem of size No, both programs take X seconds to execute. Approximately how much time would each program take to run if we double the problem size? Show your work.
2) Define what it means to have a collision in a hash table, and why we cannot usually prevent them from occurring.
Coding
1) Assume that you have a C++ string of characters. Complete the function below that converts the string into a very long (ZZ) integer such that you can subsequently convert the integer back into a unique string if you so desire.
void convert(char str[], ZZ& intval);
{
// complete function body
2) Consider the code for QuickSort below:
void quicksort(itemType a[], int l, int r)
{
int i, j; itemType v;
if (r > l)
{
v = a[r]; i = l-1; j = r;
for (;;)
{
while (a[++i] < v) ;
while (a[--j] > v) ;
if (i >= j) break;
swap(a, i, j);
}
swap(a, i, r);
quicksort(a, l, i-1);
quicksort(a, i+1, r);
}
}
Explain why the code as written will likely cause a run-time error, and correct it so that it will always run properly. Be specific and give an example that will cause it to crash.
SOLUTIONS
Fill in the Blanks Complete the statements below with the MOST APPROPRIATE words/phrases.
a) Two variations of QuickSort designed to minimize the probability of the worst case are __median of three_ and __random pivot_____.
b) The GradeSchool integer multiplication algorithm requires Theta____N2______ time for N-bit integers.
c) If I have an open addressing hash table of size M, and a cluster of size C, the probability that a random key will be Inserted into the location immediately after the cluster is ___(C+1)/M_______.
d) If I am using an RSA encryption scheme, I may DECRYPT my message first and have a receiver later ENCRYPT it for the purpose of having a _____digital signature________.
Short Answers and Calculations
1) You have two programs, Program A, which runs in time k1N and Program B, which runs in time k2log2(N) for some constants k1 and k2. Assume that for a problem of size No, both programs take X seconds to execute. Approximately how much time would each program take to run if we double the problem size? Show your work.
For Program A, since the time is linear, we know that if we double the problem size the run-time should also double. Thus we can say 2X seconds for Program A. For Program B it is more complicated, since Program B runs in logarithmic time. However we can still solve this with some math:
We know: k2log2(No) = X
And we want to solve k2log2(2No) = ?
Remembering properties of logarithms, we can rewrite the problem as follows:
k2log2(2No) = k2[log2(2) + log2(No)] = k2 + k2log2(No) = k2 + X
2) Define what it means to have a collision in a hash table, and why we cannot usually prevent them from occurring.
A collision occurs in a hash table if, for two keys, x1 and x2, h(x1) = h(x2), with x1 != x2. Collisions cannot usually be prevented, since, in most instances, the key space being used (all possible keys) is greater in size than the table size, and, by the Pigeonhole Principle, at least two distinct keys must map to the same table location.
Coding
1) Assume that you have a C++ string of characters. Complete the function below that converts the string into a very long (ZZ) integer such that you can subsequently convert the integer back into a unique string if you so desire.
void convert(char str[], ZZ& intval) // Your answers may vary
{
int size = strlen(str); // get length of string
int curr;
intval = 0; // initialize integer to 0
for (int i = size-1; i >= 0; i--) // starting at end of string to make conversion
// back simpler
{
curr = str[i]; // get current character
intval = (256 * intval) + curr;
// multiply by chosen radix and add current char value
// 256 is good if we are using the extended ASCII set
}
}
2) The loop while (a[--j] > v) ; may go past the left end of the array, since it does no bounds checking. For example, if the data in the array is initially reverse sorted, the pivot will be the smallest value in the array and the loop will not terminate within the bounds of the array. To fix this, we can either place a sentinel into location a[0] that is guaranteed to stop the loop, for ex: a[0] = 0; if all of the regular values are positive, or a[0] = v ; if the values can be anything. Alternatively, we could add another test to the loop:
while (j > l && a[--j]
> v); // it is the variable l (for left) , not the number 1.