DSA456 Midterm Radu PDF
DSA456 Midterm Radu PDF
Name
Student Number
Section
QUESTIONS:
SPECIAL INSTRUCTIONS:
As a Seneca student, you must conduct yourself in an honest and trustworthy manner in all
aspects of your academic career. A dishonest attempt to obtain an academic advantage is
considered an offense, and will not be tolerated by the College.
DSA456 Mock Test 1, Fall 2022 – version A Page 2 of 10
Question 1 [6 marks]
Find the time complexity (big-O of the running time) of the following functions:
def function1(n):
sum = 0
for i in range(0:5):
sum += i
return sum
def function2(n):
sum = 0
for i in range(0:n):
sum += i
return sum
def function3(n):
return function1(n) + function2(n)
A five-step analysis is NOT marked in this question. Please provide the final answer and annotate the code to show
your work. Use blank spaces for scratch work.
function1 is O(1)
function2 is O(n)
function3 is O(n)
Question 2 [4 marks]
In 2 or 3 sentences, describe the addition of an item to a dynamic array that is already full.
Question 3
If you have to find an element in a list, how would you take advantage of the fact that the list is sorted? Provide
separate answers for the cases where the list is represented by:
(a) Use binary search for O(log(n)) time complexity, where n is the length of the array
(b) Use linear search since binary search would take too much time due to sequential traversal. A
successful linear search would take on average n/2 comparisons, and in the worst case n
comparisons. An unsuccessful search takes n comparisons. Hence the search is linear time,
with some savings in the average case of the successful search
Question 4 [6 marks]
What is the running time of insertion sort when all elements are equal? Justify by annotating the code with time
complexities. A full five-step analysis is not required in this question.
Hint: the key difference from the general case is how many iterations will occur in line 6.
Line 6 has O(1) iterations because arr[j-1] == curr and the for j loop exits immediately when array items are equal
Lines 7-8 O(1) work
Line 4 has O(size) iterations for i
Lines 5, 9 O(1) work
Total O(size) * (O(1) + O(1) * O(1) + O(1))
Question 5
The following recursive function reverts a list in Python. For example, revert([1,2,3,4]) will return [4,3,2,1].
However, the l[1:] operation in line 5 creates a new list and copies n-1 elements in it, where n is the length of l. Also,
returning a list involves copying that list.
Let n = len(l)
Each recursive step shortens the list by 1
The base case is when l has length 0 (in line 2)
=> # levels of recursion = (n-0)/1 = n
O(n) work per level due to copying list[1:] in line 5
=> total running time is O(n) * O(n) = O(n2)
(b) [6 marks] Write a recursive function revert_rec that no longer copies lists on recursive calls and on returns.
Another function revert() will launch revert_rec() with two more integers as parameters, representing indices in the
list. Your function should have O(n) running time, where n is the size of the list. No analysis is required.
def revert(l):
revert_rec(l, 0, len(l) - 1)
return l
Hint: at each level of recursion, swap the list items at the two indices. Keep one of the items in a temporary variable
over the recursive call, so it won’t be overwritten by reverting a sublist with the recursive call.
(c) [6 marks] Use a list as a stack to write a non-recursive function that reverts a list in O(n) time, where n is the
size of the list.
Assume stack has operations .push(item) and .pop( ), where .pop( ) returns the first element of the stack and
removes it from the stack.
def revert_stack(list):
for e in list:
stack.push(e)
for i in range(len(list))
list[i] = stack.pop( )
Question 6 [6 marks]
Suppose you have an array of n elements containing only two distinct keys, true and false. Give an O(n) algorithm to
rearrange the array so that all false elements precede the true elements.
def rearrange(list):
""" ensures all False precede all True elements
assumes list contains Booleans only """
count = 0
for i in range(len(list)):
if not list[i]:
count += 1
for i in range(count)
list[i] = False
for i in range(count:len(list))
list[i] = True
Question 7
(a) [3 marks] Consider an empty stack. Show the stack after each operation of the following sequence:
(b) [3 marks] Consider an empty queue. Show the queue after each operation of the following sequence:
Stack Queue
front back
Stack
a
ba
a
ca
dca
ca
Queue
a
ba
b
cb
dcb
dc
Question 8
(a) [3 marks] Draw the linked list after deleting the element pointed to by a
(b) [3 marks] Complete the following code snippet to perform the deletion described in Part (a)
a.next.prev = a.prev_
a.prev.next = a.next_
free( a )
Question 9 [6 marks]
In this question, you may use functions from the following code (or, equivalent Python code), but you are not
allowed direct access to the private declarations in DList. Implement a member function that returns the number of
nodes of the list. Use either C++ or Python.
class DList{
Node* front_;
Node* back_;
struct Node{
int data_;
Node* next_;
Node* prev_;
Node(int data = 0, Node* nx=nullptr, Node* pr=nullptr){...}
};
public:
class const_iterator{
Node* curr_;
friend class SortedList;
const_iterator(Node* p){...}
public:
const_iterator(){...}
const_iterator operator++(){...}
const_iterator operator++(int){...}
const T& operator*() const {...}
bool operator==(const_iterator){...}
bool operator!=(const_iterator){...}
};
class iterator:public const_iterator{
...
iterator(Node* p){...}
friend class SortedList;
public:
iterator(){...}
iterator operator++(){...}
iterator operator++(int){...}
T& operator* {...}
};
DList(){...}
const_iterator begin()const{...}
const_iterator end()const{...}
iterator begin(){...}
iterator end(){...}
};
def count(dl):
curr = dl
count = 0
while curr:
count += 1
curr = inc(curr)