0% found this document useful (0 votes)
1 views

2020 Winter Midterm

The document consists of a structured exam with multiple-choice questions, pseudo code tasks, and true/false statements related to algorithms and data structures. It assesses knowledge on asymptotic analysis, algorithm efficiency, and specific implementations like queues and stacks. The exam is divided into three parts, with varying point allocations for each question.

Uploaded by

nidal aldabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

2020 Winter Midterm

The document consists of a structured exam with multiple-choice questions, pseudo code tasks, and true/false statements related to algorithms and data structures. It assesses knowledge on asymptotic analysis, algorithm efficiency, and specific implementations like queues and stacks. The exam is divided into three parts, with varying point allocations for each question.

Uploaded by

nidal aldabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ID:

Part A There are 8 multiple-choice questions in this part.


You must use the supplied multiple-choice answer sheet to record your answers for this part (Part A). Only
answers on that sheet will be considered (answers on the exam booklet itself will be discarded). You must
highlight your ID on that multiple-choice sheet as well. A correct choice for one question will get you 4
points. An incorrect answer, marking several answers or selecting nothing for a question will get you 0. If
you believe that more than one answer is correct, select the best answer.

1. What is the best asymptotic (“big-O”) characterization of the following function:


𝑓(𝑛) = (14𝑙𝑜𝑔𝑛)2 + log⁡(3𝑛 )
𝑛
a) 𝑂(3 )
b) 𝑂(𝑛2 )
c) 𝑂(𝑛)
d) 𝑂(2𝑛 )
e) 𝑂(𝑙𝑜𝑔𝑛)

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:

declare a character stack


while ( more input is available)
{
read a character
if ( the character is a '(' )
push it on the stack
else if ( the character is a ')' and the stack is not empty )
pop a character off the stack
else
print "unbalanced" and exit
}
print "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?

a) Best case 𝑂(1),⁡worst case 𝑂(𝑛).


b) Best case 𝑂(𝑙𝑜𝑔𝑛),⁡worst case 𝑂(𝑛).
c) Best case 𝑂(𝑛),⁡worst case 𝑂(𝑛).
d) Best case 𝑂(1),⁡worst case 𝑂(𝑛2 ).
e) Best case 𝑂(𝑛),⁡worst case 𝑂(𝑛2 ).

2
ID:

6. Consider the following pairs of functions: 𝑓(𝑛), 𝑔(𝑛).⁡For which pair, the functions are such that
𝑓(𝑛)⁡𝑖𝑠⁡𝑂(𝑔(𝑛))⁡⁡and 𝑔(𝑛)⁡𝒊𝒔⁡𝒏𝒐𝒕⁡𝑂(𝑓(𝑛))?

7. What is the best asymptotic (“big-O") characterization of the following function:


𝑓(𝑛) = 6𝑛⁡𝑙𝑜𝑔⁡𝑙𝑜𝑔(𝑛2 ) + 2𝑛𝑙𝑜𝑔2 𝑛 + 𝑛𝑙𝑜𝑔𝑛

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:

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 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.

1. If 𝑓(𝑛) = 5𝑛2 then 𝑓(𝑛) ∈ Ω(2𝑛 ) □T □F

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

You might also like