0% found this document useful (0 votes)
22 views4 pages

Test 1 2017

COMP201 TEST 1 2017

Uploaded by

ziaziyaee
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)
22 views4 pages

Test 1 2017

COMP201 TEST 1 2017

Uploaded by

ziaziyaee
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/ 4

Thursday, August 24, 2017

SCHOOL OF MATHEMATICS, STATISTICS


& COMPUTER SCIENCE

UNIVERSITY OF KWAZULU NATAL

WESTVILLE CAMPUS

Theory Test I

COURSE CODE: COMP201 Data Structures

Examiner: Mr E. Jembere
Marks 50 marks
Duration 1hr 20 mins

Instructions

1. This paper is four (4) pages long.


2. Answer all questions.
3. Write neatly
4. Please note that this paper has 2 bonus marks

1. Indicate whether each of the following statements is true or false;


a. The method x.delete(element) removes an element from a set or list named x.

b. You can use an index to access elements in a set.

c. If a linked list has only one node, both the head and tail reference that node.

d. The best-case time complexity for adding n elements in to a heap is O(n Log (n)).

e. The array {100, 19, 36, 17, 3, 25, 1, 2, 7} is a heap.

f. When you create an array using ArrayList x = new ArrayList(10), x.size() is 10.

g. The interfaces Set, Map, and List are all sub-interfaces to the collection interface.

h. The method remove (int index) is defined in Map.

1|Page
Thursday, August 24, 2017

i. 𝑓(𝑛) 𝑖𝑠 𝑛𝑂(1) means the function 𝑓(𝑛) is bounded above by a polynomial function.

j. If f1(n) is O(g1(n)) and f2(n) is O(g2(n)), then f1(n)*f2(n) is O((g1(n)*g2(n)).

[10]

2. Suppose that set1 (Set<Object>) is a set that contains the strings "red", "yellow", "green", and that set2
(Set<String>) is another set that contains the strings "red", "yellow", "blue". Answer the following
questions: (All the questions are independent)
a) What is set1 after executing set1.addAll(set2)?

b) What is set1 after executing set1.add(set2)?

c) What is set1 after executing set1.removeAll(set2)?

d) What is set1 after executing set1.remove(set2)?

e) What is set1 after executing set1.retainAll(set2)?

[5]

3. The program in Figure 1 creates a map with an ID number and Full name as a key/value pair for each
entry. Show the output of the code in Figure 1.

public class Test {


public static void main(String[] args) {
Map<String, String> map = new LinkedHashMap<String, String>(10, 0.75f, true);
map.put("1023", "John Doe");
map.put("1011", "Harry Potter");
map.put("1023", "Jack Martins");
map.put("2022", "Jack Martins");
map.put("1006", "Nonhlanhla Nkosi");
System.out.println("\nThe value for 1023 is " + map.get("1023"));
System.out.println("(1) " + map.get("Harry Potter"));
System.out.println("(2) " + new TreeMap(map));
}
}

Figure 1

[5]

4. Show that the worst case time complexity for the quick sort algorithm is O(𝑛2 ).
[6]

2|Page
Thursday, August 24, 2017

5. Determine the time complexity of the code snippet in Figure 2:

𝐢𝐧𝐭 𝐜𝐨𝐮𝐧𝐭 = 𝟎;

𝐟𝐨𝐫(𝐢𝐭 𝐢 = 𝟏; 𝐢 <= 𝐧; 𝐢 ∗= 𝟒 )

𝐟𝐨𝐫(𝐢𝐧𝐭 𝐣 = 𝟏; 𝐣 <= 𝐢; 𝐣 + + )

𝐜𝐨𝐮𝐧𝐭 + +;

Figure 2

[6]
6. The study the code snippet Figure 3 and answer the following questions:

public double doSomething(double x, int n) {


if (n==0)
return 1;
else if(n%2==0){
return doSomething(x, n/2)* doSomething (x, n/2);
}
else {
return doSomething (x, n/2)* doSomething (x, n/2)*x;
}
}

Figure 3

a. Determine the complexity of the algorithm in Figure 3.


b. What changes would you make to the code to make the algorithm run in logarithmic time
complexity.
[5, 2]
7. The method shown in Figure 4 removes an element at a specific position in a doubly-linked list. Study
the code and answer the following questions.
a. The program in Figure 4 removes a node at which position of a doubly linked list.

b. Which special case of deleting a node is being handled in the else if block in lines 4 – 9.

c. What is the purpose of the for-loop block in line 12?

d. The program in Figure 4 runs in O (n) time. Rewrite the code such that it runs in constant time,
0(1).

3|Page
Thursday, August 24, 2017

1 public E removeNode() {
2 if (tail == null)
3 return null;
4 else if (head==tail) {
5 DLLNode<E> temp = head;
6 head = tail = null;
7 size --;
8 return temp.element;
9 }
10 else {
11 DLLNode<E> current = head;
12 for (int i = 0; i < size - 2; i++)
13 current = current.next;
14 DLLNode<E> temp = tail;
15 tail = current;
16 tail.next = null;
17 size--;
18 return temp.element;
19 }
20 }

Figure 4

[2, 1, 2, 2]

8. Using the definition of a node given in Figure 5, write the java code for an implementation of the
following methods in a circular-doubly linked list.

public class DLLNode <E> {


E element;
DLLNode<E> next;
DLLNode<E> previous;
public DLLNode() {
next =null;
previous =null;
}
public DLLNode(E el, DLLNode<E> n, DLLNode<E> p) {
element= el;
next = n;
previous = p;
}

Figure 5

a. addFirst(E e), //adds a node at the beginning of a circular-doubly linked list.


b. addLast(E e), //adds a node at the end of a circular-doubly linked list.
[3, 3]
----------------------------------------------------------------------------------------------------------------------------------
WISHING YOU ALL THE BEST

4|Page

You might also like