You are on page 1of 8

CMPS 101 Practice Exam 1 (Exam on Oct 30)

Name:

Student Number:

CMPS 101 Practice Exam 1 (Exam on Oct 30)


100 Points Total

Closed book, + Master Theorem

cats

Oct. 21, 2013


Answer on this Exam

READ THIS FIRST.


Use separate paper for scratch work that will be unintelligible to a grader. But do write information that explains your
reasoning toward the answer if you think it may help toward part credit.
Throughout the exam, access to Abstract Data Types is only through their specified functions and procedures, which,
for IntList are essentially as given in the text and used in pa01, ho04.txt. You do not know their implementations.
Budget your time. Look quickly through the exam to get an overall idea. Do not dwell on one problem when there
are easier points to be picked up elsewhere. You have approximately one minute per point.
The Master Theorem, as it appears on page 139 (with corrections in the errata list) will be printed on the exam.

1 Induction Proofs
Following is a claimed theorem and a possible induction proof. In the questions following it, decide whether it is
correct, and explain your reasons.
Theorem In any nonempty list of positive integers, the sum of the elements is less than or equal to the product of the
elements.
Proof
The base case is one element. Then the sum = the product.

For lists having n > 1 elements, let E be the first element. Let Srest and Prest be the sum and product of the remaining
elements of the list, respectively. By the inductive hypothesis, Srest  Prest . Since E  1, E + Srest  E  Prest .
QED
A.
B.
C.

Is the theorem correct (yes or no)?


Is the proof correct (yes or no)?
Briefly explain your reasons for parts (A) and (B).

Grading note: The grade is based on the complete answer, so a correct yes/no for a totally bogus reason doesnt get
any points, while a wrong yes/no for an almost correct reason would get some part credit.

2 Abstract Data Type Design


A.

What are the three major categories of ADT operations? Briefly explain the purpose of each category.

B. You are about to write a C program that uses a SymbolTable abstract data type. Another programmer
has written symtab.h, symtab.c, and symtabtester.c. Rank these files from best to worst as sources of
information about how to use this ADT.
C (2 pts). In reading the specification for a Stack ADT, you see
void pop(Stack s) Precondition: s is not empty. : : :
How would you normally expect to find out within a client program whether this precondition is satisfied?
CMPS 101 Fall, 2013

Handout 07

CMPS 101 Practice Exam 1 (Exam on Oct 30)

3 Abstract Data Type Design


What is structurally wrong with this specification from an information hiding (or data encapsulation) standpoint? For
full credit, find 2 distinct mistakes. Show revised prototypes and brief explanations that correct the problems.
(Brevity is not the mistake we are looking for.) The question might be phrased with either C or Java syntax. Both are
shown in this example.
C

SYNTAX

typedef struct QueueObj * Queue;


typedef void * QueueElt;
Queue
makeEmptyQueue(void);
/* returns a queue with no elements in it */
void
enqueue(Queue q, QueueElt newElt);
/* puts newElt at end of q */
QueueElt dequeue(Queue q);
/* returns element at front of q and removes it */
JAVA SYNTAX
/** returns a queue with no elements in it */
Queue
makeEmptyQueue();
/** puts newElt at end of q */
void
enqueue(Queue q, QueueElt newElt);
/** returns element at front of nonempty q and removes it */
QueueElt dequeue(Queue q);
Hint: c-adt.pdf is a good source for this question.

4 Big-
Give Big- expressions for each problem; for full credit they must be fully simplified.
n
X
k

=1

n
X

=1

lg(k) =

n=2
X
i i

=1

1) =

5 log
n
X2
j

=1

2j =

Short-answer questions will require knowing O, , and


. On each line is a correct statement and a false statement.
Circle the correct statement.

log(n) 2 O(n2 )
If f (n) 2 (nE ); then f (n) 2
(nE ):
f (n)
! 0; then f (n) is in O(g(n)):
If
g (n)
n

CMPS 101 Fall, 2013

log n 2
(n2 )
If f (n) 2
(nE ); then f (n) 2 (nE ):
f (n)
! 0; then f (n) isnt in O(g(n)):
If
g (n)
n

Handout 07

CMPS 101 Practice Exam 1 (Exam on Oct 30)

Multiple Choice 10 to 20 questions, all topics may be asked: Circle the correct choice out of four to complete
each statement for which you know the answer.
2 points per correct answer, 0 points if blank, and 1 points if wrong answer.

1. O( n) includes the functions


(a)
(b)
(c)
(d)

1 pn and 1 n
2
100
2pn log n and 2pn
1 pn log n and 1 pn
2
2
2pn and 10 log n

2. If f (n) is in (g (n)), then

(a) f (n) is in O(g (n)) and also in


(g (n)).

(b) f (n) is in
(g (n)) but not in O(g (n)).

(c) f (n) is in O(g (n)) but not in


(g (n)).

(d) f (n) is in O(g (n)) nor


(g (n)).
Master Theorem

Let b  1 and c > 1 be constants. Let T (n) be defined on the positive integers by

( ) = b T (n=c) + f (n)
log2 b . Then T (n) can be bounded asymptotically as
with T (0) = 1. Further, define the critical exponent, E =
log2 c
T n

follows:

1. If f (n) 2 O nE  for some positive , then T (n) 2  nE , which is proportional


to the number of leaves in the recursion tree. 
2. If f (n) 2  nE , then T (n) 2  nE log(n) , as all node depths contribute about
equally.

3. If f (n) 2
nE + for some positive , and b f (n=c)  r f (n) for some r < 1 and
large enough n, then T (n) 2 (f (n)), which is proportional to the nonrecursive
cost at the root of the recursion tree.
Note that log2 3  1:59 and 1= log2 3  0:63 and log2 5  2:32 and 1= log2 5  0:43.

5 Recurrence Equations
Give Big- expressions for each problem; for full credit they must be fully simplified. (Show work to ensure part
credit.)
 

( ) = 1 + T n2

T n

T n

(1) = 1

T n

 

( ) = n + T n2

T n

 

( ) = n + 2 T n3

T n

CMPS 101 Fall, 2013

 

(1) = 10

( ) = 1 + 2 T n2

(1) = 10

(1) = 1

 

( ) = n + 2 T n2

 

(1) = 1

( ) = lg(n) + 2 T n3

T n

(1) = 1
Handout 07

CMPS 101 Practice Exam 1 (Exam on Oct 30)

6 Recursion Trees
Show the top three levels of the recursion tree for each recurrence equation. The top level should have one node that
represents T (n). Do not solve.
 

(A)
(C)

 

(B)

( ) = T k2 + T k3 + C k

T k

( ) = 2T

T k

( ) = T (k 1) + T (k 2) + B

T k

+Ck

7 Deriving Recurrence Equations


Given a simple recursive procedure, derive a recurrence equation for its worst-case running time. For example
double findSum(double[] A, int low, int hi)
{
int n = hi - low;
double ans;
if (n <= 0)
ans = 0;
else if (n == 1)
ans = A[low];
else
{
int mid = (hi + low) / 2;
int left = findSum(A, low, mid);
int right = findSum(A, mid, hi);
ans = left + right;
}
return ans;
}
Find a recurrence for T (n).

8 Java and C
Some symbols almost always mean the same in Java and C, while a few often mean something different. Among these
symbols, a few will be selected for you to decide whether they are usually the same meaning or not.
! % & * ( - + = { | [ : " ; < ? , . /
The selections will be common symbols, not obscure ones.

9 Other problems
Some problems might be similar to Exercises 3.7, 3.8, 3.10, 3.12. However, exact solutions of recurrences will not be
asked, only asymptotic orders.

CMPS 101 Fall, 2013

Handout 07

CMPS 101 Practice Exam 1 (Exam on Oct 30)

10 Unix and Editor Skills


See ho05.txt. This will be 10 points. On multiple-choice questions, the wrong answer will count negative points,
whereas leaving it blank is 0. (So dont plan on guessing.)

11 Quicksort and Partition


A. Illustrate the evolution of quicksort on the array below. Show one complete round of partitioning per line (i.e.,
one subroutine call).
Reread the above line. Reread the above line. Reread the above line.
For each succeeding picture, indicate what subrange was worked on and its partitioning key (also called pivot). You
should use the method of partitioning in the main text.
Show important boundaries as lines between arrays. You can use the same array line for independent subproblems.
Reread the above line. Reread the above line. Reread the above line.
Do not rewrite subranges that are completed.

1 2 3 4 5 6 7 8 9 10 11 12
32 56 30 70 81 31 11 21 12 41

B.

Give the asymptotic growths rates (big ) for Quicksort in worst case? average case? best case?

CMPS 101 Fall, 2013

Handout 07

CMPS 101 Practice Exam 1 (Exam on Oct 30)

12 Merge and Mergesort


A. Illustrate the evolution of Mergesort on the array below. For each subproblem, indicate what subranges were
worked on by drawing boundaries between input and output array. Independent subproblems may be shown using
the same array line. Explain a few examples of element movement, to give the idea.

1 2 3 4 5 6 7 8 9 10 11 12
41 56 32 30 70 81 31 11

B.

Give the asymptotic growths rates (big ) for Mergesort in worst case? average case? best case:?

C. Describe in correct sequence the compares and moves that merge does on the two arrays below. (Just showing
the result is not enough!)

1 2 3 4
01 04 07 20

1 2 3 4
02 21 23 26

1 2 3 4 5 6 7 8 9
13 Heap Operations
A.

Illustrate the operation of a single deleteMax by showing successive pictures. Do not use accelerated fixHeap().

Reread the above line. Reread the above line. Reread the above line.

CMPS 101 Fall, 2013

Handout 07

CMPS 101 Practice Exam 1 (Exam on Oct 30)

Indicate clearly what elements get compared and/or moved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
75 45 70 40 30 15 10 20 35 25

B.

Illustrate the operation of heapInsert for key 50 by showing successive pictures.

Indicate clearly what elements get compared and/or moved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
75 45 70 40 30 15 10 20 35 25

C.

Suppose a heap holds n elements. What are the worst-case big  running times of deleteMax? heapInsert?

CMPS 101 Fall, 2013

Handout 07

CMPS 101 Practice Exam 1 (Exam on Oct 30)

14 Sorting Debate
To prepare for a debate in which you do not know which side you will need to argue, prepare a 2D table that gives one
or two significant advantages (not an exhaustive comparison) of the sorting method (in each row, leftmost column)
over its competitors (listed in subsequent columns).
1. The four methods to compare are heap, insertion, merge, and quick sorts.
2. The advantages may be in time and/or space.
3. You may consider best, average, and worst cases to make your debating point.
4. Be specific about growth rates (()) involved for the compared methods.
5. Constant-factor advantages are not considered significant for this question.
6. The correct answer might be none for certain cases. None is acceptable if the only differences are in constant
factors.
There should be a total of 15 entries among the 12 nondiagonal cases, counting nones, if any.

Example: in bubble sorts row and wander sorts column the entry might be (n2 ) worst case time vs. (n!).
over
heap sort

over
insertion sort

over
merge sort

over
quick sort

advantage of
heap sort

advantage of
insertion sort

advantage of
merge sort

advantage of
quick sort

CMPS 101 Fall, 2013

Handout 07

You might also like