Midterm
Midterm
ID:
Part A [32 Points] There are 8 multiple-choice questions in this part.
To choose an answer, simply draw a circle around the bullet (.) of the answer chosen (if any!).
A correct choice for one question will get 4 point. An incorrect answer or marking several
answers for a question will get —1. The minimum total mark for this part is 0.
* An Algorithm C1(n) uses 10n log 2 n operations and C2(n) uses n 2 + 512
operations. Which of the following values for n o is the smallest such that C2(n) >
C1(n) for all n > no?
64 • 128 • 16 • 32
a t R a n k
replaceAtRank
remove
elemAtRank
2
Student Name: ...................................................................................................... ID:
* Consider the following List ADT operations (where the pi's represent positions): p1
—insertFirst(8), p 2=insertAfter(p1,5), p3=insertBefore(p2,3), p4=insertFirst(9), remove(p 4),
swapElements(p i,p2), replaceElement(p 3, 7), p5=insertAfter(first0,2). Which of the
following options describes the final list in a correct way?
(8,5,3,9) • (5,2,7,8)
A,B,D,E.H,J,C,F,K,G
D,H,J,E,B,K,F,G,C,A
A,B,E,H,J,K,D,C,F,G
D,B,H,E,J,A,K,F,C,G
* Which of the following options list the functions in correct non-decreasing order
if they are compared by asymptotic growth?
log n, n, n log n, n 2 + log n, n — n 3 + 7n 5 , 2', nn, n!
log n, n, n log n, n 2 + log n, n — n 3 + 7n 5 , 2' n!, nn
log n, n, n log n, n 2 + log n, n — n 3 + 7n 5 , n1 2' nn
log n, n, n log n, n — n 3 + 7n 5, n2 + log n, 2', nn, n!
* What is the correct sequence of insert and/or removeMin operations on heap
A that will transform it into heap B (as shown below)?
A B
insert(7),removeMin()
5 5
3
Student Name: ...................................................................................................... ID:
Part B [44 Points]
B.1 [2 ❑ Points] Write in pseudo code a tail-recursive algorithm "reverse" using the Sequence
ADT that creates from a given sequence a new copy in reverse order. For instance, if S1
represents the sequence (1,2,3), then S2 := reverse(S1) returns a sequence that represents
(3,2,1) but leaves S1 unchanged.
3. [4 Points] What are the worst-case runtimes of your above algorithms (use the big-
Oh notation). Justify your answers.
4
Student Name: ...................................................................................................... ID:
B.2 [24 Points] The Towers of Providence is a variation of the classical Towers of Hanoi
problem. There are four pegs, denoted A, B, C, and D, and n disks of different sizes.
Originally, all the disks are on peg A, stacked in decreasing size from bottom to top.
Our goal is to transfer all the disks to peg D , and the rules are that we can only move
one disk at a time, and no disk can be moved onto a smaller one. Write a recursive
algorithm in pseudo code that solves the Towers of Providence problem.
Hint: use the operator `Sz' to concatenate elements and lists, the functions first(L)
which returns the first element of a list L, rest(L) which return the list L without its
first element, and the predicate empty(L) which is true if L does not contain an element.
5
Student Name: ................................................................................................... ID:
Part C [24 Points] For each of the 4 questions in this part, mark T if the given statement
is ALWAYS true. Otherwise mark F and justify your answer. If you do not justify the
FALSE case you will lose marks. There is no penalty for selecting the wrong answer.
Hint: A correct counter example or the correct specification will give you the full mark.
If f (n) and g(n) are both 0 (h(n)) , t he n f (n) + g(n) is not 0 (140 . ❑ T
❑F