2020 Winter Midterm
2020 Winter Midterm
2. Give the best asymptotic (“big-Oh”) characterization of the worst case and the best case time
complexities of the algorithm DoAgain(A, n).
3. You have implemented the queue with a linked list, keeping track of a front node and a rear node with
two reference variables. Which of these reference variables will change during an insertion into a
NONEMPTY queue?
a) Neither changes
b) Only front changes.
c) Only rear changes.
d) Both change.
1
ID:
4. Here is an incorrect kind of pseudo code a student provided for the algorithm which is supposed to
determine whether a sequence of parentheses is balanced:
Which of these unbalanced sequences does the above code think is balanced?
a) ((())
b) ())(()
c) (()()))
d) (()))()
5. Consider the algorithm Multiply(A,n) below. A is an array of size n storing integer values. What is the
best characterization of the best and worst case asymptotic time complexity of the following
algorithm?
2
ID:
6. Consider the following pairs of functions: 𝑓(𝑛), 𝑔(𝑛).For which pair, the functions are such that
𝑓(𝑛)𝑖𝑠𝑂(𝑔(𝑛))and 𝑔(𝑛)𝒊𝒔𝒏𝒐𝒕𝑂(𝑓(𝑛))?
a) 𝑂(𝑛𝑙𝑜𝑔2 𝑛)
b) 𝑂(𝑛𝑙𝑜𝑔𝑛)
c) 𝑂(𝑛2 𝑙𝑜𝑔𝑛)
d) 𝑂(𝑛3 𝑙𝑜𝑔𝑛)
e) 𝑂(𝑛𝑙𝑜𝑔(𝑛2 ))
8. Suppose we have a circular array implementation of the queue class, with ten items in the queue
stored at data [2] through data [11]. The current capacity is 42. Where does the insert method place
the new entry in the array?
a) data[1]
b) data[2]
c) data[11]
d) data[12]
3
ID:
Part B
Important Note
❖ Efficiency refers to time and memory.
❖ You will be graded as follows:
➢ A mark (100%) if you provide a correct and most efficient algorithm.
➢ A mark (%80) if you provide a correct and second efficient algorithm.
➢ A mark (%50) if you provide a correct and missed to consider the efficiency of
the algorithm.
B.1 [33 Points] A palindrome is a string that reads the same forward and backward, capitalization and
space are ignored. For example deed, go dog, level…. are palindromes.
a) [10 Points] Write an iterative algorithm in pseudo code that tests whether a string is a
palindrome.
b) [10 Points] Write a recursive algorithm in pseudocode that tests whether a string is a palindrome.
4
ID:
c) [8 Points] Describe how you could use a Stack or Queue to check whether a string is a
palindrome, and write the pseudo code for that.
d) [5 Points] What are the worst-case runtimes of above algorithms (a), (b), (c) (use the big-
Oh notation). Justify your answers.
5
ID:
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 4⁄6 of the mark. There is no
penalty for selecting a wrong answer. Hint: a correct counter example and/or correct specification will
give you better marks. A correct answer will get you 6 points.
2. The worst-case asymptotic running time for the best algorithm for finding something in a sorted
array? is 𝑂(𝑛) □T □F
3. The worst-case asymptotic running time of finding and removing all values greater than 12 from a
stack implemented with a linked-list (leaving the rest of the stack in its original order) is O(1)
□T □F
4. Performing a remove operation at the tail in a list ADT implemented as a singly LinkedList that
keeps track of the head, is very efficient: performed in a constant time O(1).
□T □F