CS 1501 Previous Midterm Exam
The following exam was given
in a previous term of CS 1501. Use it as
a guide for the types of questions that you may see on this term's midterm
exam, and for the content of some of the questions. However, since the material is not covered in
the same order from term to term, there may be some material on your midterm
that is not covered in the exam below (and vice versa). To ensure
that you study all pertinent material, refer to the Online Syllabus,
the Online
Notes and your hand written notes.
After giving the questions
below a good try, take a look at the solutions
to see how you did.
1) Fill in the Blanks (20 points -- 2 points each). Complete the statements below with the MOST APPROPRIATE words/phrases.
a)
If a program's run-time can be modeled by the
function 10N3 + N2(8N
+ 2N2), the program's Theta runtime growth rate is
_______________________.
b)
An algorithm that is known to run in time Theta(N2)
requires 8 seconds to run on a problem of size K. How long will the algorithm take to run on a
problem of size 2K? ______________________.
c)
A ___________________________________ is similar to a
binary search tree, but branches to the left or right are made based on the
current bit in the key (0 or 1).
d)
Assume that I have 256 32-bit keys in a multiway trie in which 4
bits are considered at a time. The
worst case height of this tree is ___________________ and the average case
height is _____________________.
e)
The InsertionSort algorithm has a worst case
asymptotic runtime of ____________________________ and a best case
asymptotic runtime of _____________________________.
f)
The recurrence relation for QuickSort in
the best case is ___________________________________.
g)
Assuming a pattern of length M is searched for
within a text string of length N, the normal case run-time of the
brute force (naοve) string matching algorithm is ______________________
and its worst case run-time is __________________________.
h)
The simple divide and conquer integer
multiplication algorithm has a run-time of ___________________ and Karatsuba's
improved algorithm has a run-time of ____________________.
i)
Using the recursive, efficient GCD
algorithm that we discussed in class, finish the equation sequence (show each
recursive call and the result): GCD(108, 63) =
______________________________________________.
j)
Given a substitution cipher on an alphabet with
S characters, there are ______________________________ possible keys for the
cipher.
2) True/False (10 points -- 2 points each).
Indicate whether each of the following is TRUE or FALSE, explaining why in an informative way for false answers.
a)
Pruning is a technique that improves exhaustive search algorithms by reducing
the number of execution paths that are followed.
b)
If I am using hashing to store 1000 arbitrary students (some may
graduate and some may enroll) based on their names, I can guarantee that no
collisions occur as long as I keep my hash table at least twice the size of
my data (or over 2000).
c)
In the Rabin-Karp string-matching algorithm, we know two strings
match when their hash values match.
d)
If someone comes up with an efficient, polynomial-time factoring
algorithm, the RSA encryption scheme will no longer be useful.
e)
The Miller-Rabin Witness algorithm is used to verify the
authenticity of sent messages.
3)
(8 points) Consider an emtpy de la Briandais Tree (dlB) which uses the lower
case letters (plus a string termination character) as its alphabet. Also consider the following 6 strings: walker wall
walk walnut wilt
silt
Draw the dlB that results after inserting the
strings shown in the order shown.
4)
(6 points) Consider the forest of
single-node trees with frequencies shown below.
Draw the Huffman tree that would result from this forest of
nodes. Show your work.
5) (8 points) Consider the chessboard below with the queens
in the marked locations. Assume that we
are using the recursive algorithm discussed in class, and that the
current recursive call is attempting to place a queen in column 6. Explain in detail the sequence of steps
that occur until the first instance in which a queen is actually placed onto
the board, or until a queen is moved from a previous position to a new
valid location (in other words, continue until a queen is placed somewhere new,
but not necessarily in column 6). In any
case, indicate where the queen is finally placed and why. Assume that I know nothing about the
algorithm or the theory behind it, and that you are teaching it to me with your
explanation, so be VERY detailed.
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
0 |
Q |
|
|
|
|
|
|
|
1 |
|
|
|
Q |
|
|
|
|
2 |
|
Q |
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
4 |
|
|
|
|
|
Q |
|
|
5 |
|
|
Q |
|
|
|
|
|
6 |
|
|
|
|
Q |
|
|
|
7 |
|
|
|
|
|
|
|
|
6)
(8 points 4 + 4) In Assignment 1, you implemented your dlB search() algorithm to
return three possible values:
1 indicates that the key is not
in the dlB and it is not a prefix of any word in the dlB
0 indicates that the key is not
in the dlB, but it IS a prefix of a word in the dlB
1 indicates that the key is
in the dlB
Let's see if this really
helps improve the searching of a Boggle board for valid words. Let's take an extreme case and assume that in
our 4x4 board of letters there are NO valid words, and, further,
that there are not even any valid prefixes to words. For example, all locations in the board could
be the letter x, and we'll assume that no words in our dictionary begin with
the letter x. Also, to simplify things,
assume we are only looking for words that are 1 or 2 letters in length.
a)
Given the search implementation above, how many calls to search()
in the dlB will we have to do for our 4x4 board? Justify your answer.
b)
Assume now that we only have a two value search 0 indicates
not found and 1 indicates found. Given
the same extreme case for our board, how many calls to search() in the
dlB will we have to do? Justify your
answer.
7) (8 points) Consider the QuickSort
algorithm that we discussed in class. Assume
that a separate function / method partition is already defined
to partition the array as we discussed in lecture, and it also returns the
index of the location of the pivot after partition has been completed. Write the Java or C++ code for QuickSort,
using one of the headers below.
public
static void QuickSort(int [] A, int left, int right) // Java
void QuickSort(int A[], int left, int right) // C++
8) (6 points) You would like to send a
message to your friend such that only he/she can read it and such
that he/she can verify that it is definitely from you and hasn't been
tampered with. Explain in detail
how this can be done using only RSA.
Assume that any necessary keys have been authenticated to their
appropriate owners.
9) (8 points) Consider the SCTable class that you
implemented in Assignment 2 (using separate chaining). Your table might look something like the
(small) one shown below. Using either
Java or C++, write the find() method (function) for this class. Recall that find() will search the table
for a key (an int), returning true if it is found and false otherwise. Assume your table array variable is T
and that the intnode objects have two fields an int that is
your data and a next that is a reference/pointer to the next node. Also assume that the hash function for the
table, h(x), has already been defined so you can call it in your code
without rewriting it here. Use one of
the two headers below:
public
boolean find(int key) // Java
bool
find(int key) // C++
T
0 |
29 15 |
||||||||
1 |
23 |
||||||||
2 |
|
||||||||
3 |
11 32 |
||||||||
4 |
|
||||||||
5 |
20 |
||||||||
6 |
|
10)
(8 points 4 + 4) Consider the Boyer Moore String Matching algorithm to find
a pattern of length M within a text string of length N.
a)
Considering only the mismatched character (MC) heuristic that we
discussed in lecture, state the best case and worst case number
of comparisons to complete a search for a pattern. For each case, give a pattern string and a
text string that will produce that case.
b)
Give the skip array for the following string:
RADIOHEAD
11)
(10 points 6 + 4) Consider the recursive divide and conquer algorithm to calculate XY,
where X and Y are (very large) N bit integers (potentially hundreds of
bits). Below are two possible
implementations of this algorithm.
Assume that type xlong represents arbitrary length integers.
VERSION A |
VERSION B |
xlong pow(xlong base,
xlong exp) { if (exp == 0) return 1; xlong temp = pow(base, exp/2); if (exp % 2 == 0) return (temp * temp); else return (base * temp * temp); } |
xlong pow(xlong base,
xlong exp) { if (exp == 0) return 1; if (exp % 2 == 0) return (pow(base, exp/2) * pow(base, exp/2)); else return (base * pow(base, exp/2) * pow(base, exp/2)); } |
a)
Assuming that base and exp are N-bit integers, in
the worst case how many total function calls (in terms of N) will
version A require until it completes execution?
Justify your answer. Assuming
that multiplication of N-bit integers takes Theta(N2), what
is the overall Theta run-time for Version A in the worst case?
b)
Again assuming that base and exp are N-bit integers,
in the worst case how many total function calls (in terms of N) will
version B require until it completes execution?
Thoroughly justify your answer.