You are on page 1of 11

Notre Dame University-Louaize

Computer Science Department


CSC 313 Data Structures and Algorithm Analysis
Exam 1, Fall 2013
Name and ID:
This exam has 31 questions for a total of 49 points.
1. (1pts) Order the following functions from fastest to slowest
A. n20 ,2n ,n log n, n2
B. 2n ,n20 ,n2 ,n log n
C. n20 ,n2 ,2n ,n log n
D. n20 ,2n , n2 ,n log n
2. (1pts) Which of the following functions grows fastest?
A. n + log n
B. n log n
C. n log n
D. n
E. n + 100 log n
3. (1pts) Which of the following functions grows fastest?
A. 2n + n
B. 2n log n
C. 2n 2n
D. 23n
E. 100 2n
4. (1pts) Check all that apply
A. log n2 = (log n)
B. log n2 = O(log n)
C. log n2 = o(log n)
D. log n2 = (log n)
5. (1pts) Check all that apply

A. n = ( n)

B. n = ( n)

C. n = ( n)

D. n = O( n)

CSC 313 Data Structures

Exam1 Fall 2013

2 of 11

6. (1pts) Select the correct answer


A. 2n = (22n )
B. 2n = o(22n )
C. 2n = (22n )
D. 2n = (22n )
7. (1pts) The worst case complexity of searching for a element x in an unordered array A of n
numbers is
A. O(n)
B. O(n log n)
C. O(log n)
D. O(1)
8. (1pts) The worst case complexity of searching for an element x in an ordered array A of n
numbers is
A. O(n)
B. O(n log n)
C. O(log n)
D. O(1)
9. (1pts) The worst case complexity of searching for an element x in an ordered linked list L of
n numbers is
A. O(n)
B. O(n log n)
C. O(log n)
D. O(1)
10. (1pts) Let A be an unordered array of n numbers and capacity > n. The worst case complexity of inserting an element x in position i < n is
A. O(1)
B. O(log n)
C. O(n log n)
D. O(n)

Page 2

CSC 313 Data Structures

Exam1 Fall 2013

3 of 11

11. (1pts) Let A be an ordered array of n numbers and capacity > n. The worst case complexity
of inserting an element x in its proper position is
A. O(1)
B. O(log n)
C. O(n log n)
D. O(n)
12. (1pts) The complexity of computing the expression xn is at most
A. O(1).
B. O(log n).
C. O(n).
D. O(n log n).
13. (1pts) In a stack the elements are accessed in (check all that apply)
A. First in first out
B. Last in first out
C. First in last out
D. Last in last out
14. (1pts) In a queue the elements are accessed in (check all that apply)
A. First in first out
B. Last in first out
C. First in last out
D. Last in last out

Page 3

CSC 313 Data Structures

Exam1 Fall 2013

4 of 11

15. (1pts) When comparing the doubly and singly linked list implementations we find the doubly
linked list implementation
A. Saves space at the expense of some additional time
B. Saves time on some operations at the expense of additional space
C. Saves neither space nor time but it is easier to implement.
D. Saves both space and time.
16. (1pts) When the sequence push(1),push(2),push(3),pop(),push(4),push(5),push(6),pop(),push(7)
the stack, from bottom(left) to top(right) will look like
A. 1,2,3,4,5
B. 1,2,4,5,7
C. 1,2,4,5,6
D. 1,3,5,6,7
17. (1pts) Let L be a linked-list and head be the pointer to its first node. The worst case complexity of printing the k th element given head is
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
18. (1pts) Let A[n] be an array. The worst case complexity of printing the k th element in an array
is
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)

Page 4

CSC 313 Data Structures

Exam1 Fall 2013

19. (1pts) What is the complexity of the following code fragment?


sum=0;
for(int i=1;i<=n;i++){
sum=sum+1;
}
A. O(log3 n1/2 )
B. O(n1/2 )
C. O(log n)
D. O(n)
20. (1pts) What is the complexity of the following code fragment?
sum=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
sum=sum+1;
}
}
A. O(n log n)
B. O(log)
C. O(n)
D. O(n2 )
21. (1pts) What is the complexity of the following code fragment?
sum=0;
for(int i=1;i*i<=n;i=3*i){
sum=sum+1;
}
A. O(n1/3 )
B. O(n1/2 )
C. O(log n)
D. O(n)

Page 5

5 of 11

CSC 313 Data Structures

Exam1 Fall 2013

6 of 11

22. (1pts) What is the complexity of the following code fragment?


sum=0;
for(int i=1;i<=n;i++){
j=1;
while(j<i){
j*=2;
sum+=1;
}
}
A. O(log n)
B. O(n log n)
C. O(n2 )
D. O(n)
23. (1pts) Assuming that the initial input is always 1 what is the complexity of the following
function?
int function(int n){
if(n==1)return 1;
sum=0;
for(int i=1;i<=n;i++)
sum=sum+1;
return sum*function(n/2);
}
A. O(log n)
B. O(n log n)
C. O(n2 )
D. O(n)
24. (1pts) Assuming that the initial input is always 1 what is the complexity of the following
function?
int func(int *A,int s, int e){
if(n==e)return 1;
for(int i=s;i<=e;i++)
A[i]=2*A[i];
int size=e-s;
return func(s,size/2)+func(size/2+1,e)+(s+size/4,e-size/4);
}

Page 6

CSC 313 Data Structures

Exam1 Fall 2013

A. O(log2 n)
B. O(n log3 n)
C. O(n3 )
D. O(nlog2 3 )

Page 7

7 of 11

CSC 313 Data Structures

Exam1 Fall 2013

8 of 11

25. (1pts) We say that a problem is O(f (n)) if


A. The best algorithm known to us that solves the problem is O(f (n)).
B. The worst algorithm known to us that solves the problem is O(f (n)).
C. The best algorithm known to us that solves the problem is (f (n)).
D. The worst algorithm known to us that solves the problem is (f (n)).
26. (1pts) We say that an algorithm is (f (n)) if
A. It is O(f (n)) and o(f (n)).
B. It is (f (n)) and (f (n)).
C. It is O(f (n)) and (f (n)).
D. It is o(f (n)) and (f (n)).
27. (2pts) Consider the problem of searching for an element x in an unordered array A of n
numbers. Then asymptotically
A. The best, average, and worst cases are the same.
B. The best case is better than the average and worst case.
C. The best and average cases are better than the worst case.
D. The best case is better than the average case and the average case is better than the
worst case.
28. (1pts) When a pointer requires 4 bytes and a data element requires 4 bytes, the singly linked
list implementation (ignore the sentinel nodes) requires less space than the array based list
when the array is
A. Less than 1/4 full
B. Less than 1/3 full
C. Less than 1/2 full
D. Less than 3/4 full
29. (1pts) Let t be a pointer to a node. What is the complexity of finding the node that is previous
to t when using a doubly-linked list
A. O(n log n)
B. O(n)
C. O(log n)
D. O(1)

Page 8

CSC 313 Data Structures

Exam1 Fall 2013

9 of 11

30. (1pts) Let t be a pointer to a node. What is the complexity of finding the node that is previous
to t when using a singly-linked list
A. O(n log n)
B. O(n)
C. O(log n)
D. O(1)

Page 9

CSC 313 Data Structures

Exam1 Fall 2013

10 of 11

31. A list ADT is implemented as a singly-linked list of Nodes WITHOUT sentinel nodes. A node
is declared as
s t r u c t Node {
Node n e x t ;
int data ;
Node ( i n t x , Node n ) : d a t a ( x ) , n e x t ( n ) { }
};

The variable head is a pointer to the first node of the list. An empty list has head=NULL.
Also head is a global variable that is accessible by all functions including the ones you are
asked to write.
(a) (6pts) Write the C++ code for the function insert to insert a new value after a given
node whose pointer is t.You can assume that t is a valid pointer in the list.
(b) (6pts) Write the C++ code for the function erase to delete a node whose pointer is t.
You can assume that t is a valid pointer in the list.

Page 10

CSC 313 Data Structures

Exam1 Fall 2013

11 of 11

(c) (6pts) Write the C++ code for the function printPrevious to print the value of the node
previous to a given node whose pointer is t. You can assume that t is a valid pointer in
the list.

Page 11

You might also like