0% found this document useful (0 votes)
673 views10 pages

DSA456 Midterm Radu PDF

The document is a mock test for a Data Structures and Algorithms course. It contains 9 questions testing concepts like time complexity analysis, dynamic arrays, binary search, sorting algorithms, recursion, stacks, queues, and linked lists.

Uploaded by

navneetkaur12482
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)
673 views10 pages

DSA456 Midterm Radu PDF

The document is a mock test for a Data Structures and Algorithms course. It contains 9 questions testing concepts like time complexity analysis, dynamic arrays, binary search, sorting algorithms, recursion, stacks, queues, and linked lists.

Uploaded by

navneetkaur12482
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/ 10

Test 1

TERM COURSE NAME COURSE CODE VERSION

Fall Data Structures and Algorithms in C++ DSA456 A

Name

Student Number

Section

DATE: Friday, Oct 7, 2022

TIME ALLOWED: 1 hour 40 min (actually 2 h 30 min)

QUESTIONS:

TOTAL MARKS 60 MARKS

PROFESSOR(S): Radu N (using a previous test as a model)

SPECIAL INSTRUCTIONS:

1. A reference sheet is permitted


2. A non-programmable, non-communicating calculator is allowed
3. A dictionary is allowed
4. Answer all questions in the space provided on the test paper

This test includes a cover page, plus 9 pages of questions.

SENECA’S ACADEMIC HONESTY POLICY

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.

Reserve an area of memory that is double the capacity of the array.


Copy the array into the first half of the new area.
Free the memory of the previous array.

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 3 of 10

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) [2 marks] an array

(b) [2 marks] a linked list

(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))

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 4 of 10

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.

(a) [4 marks] Determine the time complexity of the revert() function

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)

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 5 of 10

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

def revert_rec( l, i, j):


""" reverts the sublist l[i:j+1] (exclusive of j+1 per Python convention) """
if i < j:
temp = l[i]
l[i] = l[j]
l[j] = temp
revert_rec( l, i+1,j-1 )

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 6 of 10

(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( )

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 7 of 10

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

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 8 of 10

Question 7

(a) [3 marks] Consider an empty stack. Show the stack after each operation of the following sequence:

push_front(a), push_front(b), pop_front(), push_front(c), push_front(d), pop_front()

(b) [3 marks] Consider an empty queue. Show the queue after each operation of the following sequence:

push_front(a), push_front(b), pop_back(), push_front(c), push_front(d), pop_back()

Stack Queue

front back

Stack
a
ba
a
ca
dca
ca

Queue
a
ba
b
cb
dcb
dc

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 9 of 10

Question 8

The diagram below shows the current state of a linked list.

(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 )

Seneca College School of SDDS


DSA456 Mock Test 1, Fall 2022 – version A Page 10 of 10

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)

assuming inc is a method instead of ++

Seneca College School of SDDS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy