Professional Documents
Culture Documents
Com 124 Exam 2019 Regular Marking Scheme
Com 124 Exam 2019 Regular Marking Scheme
MARKING SCHEME
1. MULTIPLE CHOICE
i. A
ii. C
iii. B
iv. A
v. C
vi. A
vii. A
viii. D
ix. B
x. C
xi. D
xii. C
xiii. A
xiv. B
xv. B
xvi. C
xvii. C
xviii. C
xix. C
xx. C
(1 mark for each correct option, Total = 20 marks)
2(a) Differentiate between algorithm and data structure (2 marks)
(b) Given the binary tree below, answer the following questions:
14
/ \
2 11
/ \ / \
1 3 10 30
/ /
7 40
1
i. How many leaves does it have? List them (2½ marks)
ii. How many of the nodes have at least one sibling? List them. (3½ marks)
iii. What is the value stored in the parent node of the node containing 30? (½ mark)
iv. How many descendants does the root have? (½ mark)
v. What is the depth of the tree? (½ mark)
vi. How many children does the root have? List them. (1 mark)
Answer.
2(a)
Algorithm only specifies the steps for processing data on the computer while data structure
specifies the arrangement of data on the memory as wells as operations that can be performed
on the data.
While algorithms does not determine the data structure to be used for keeping the data, choice
of data structure will determine the operations ( or algorithms ) that can be used in processing
the data.
Efficiency of algorithms is measured using time complexity while efficiency of data structure
is measured using space complexity.
(2 marks for clearly differentiating between the two terms)
(b)
i. How many leaves does it have?
Number of leaves = 4. (½ mark)
The leaves nodes have the following keys: 1, 3, 7, 40 (½ mark each, Total = 2 marks)
iii. What is the value stored in the parent node of the node containing 30?
Value stored in the parent node of the node containing 30 = 11. (½ mark)
vi. How many children does the root have? Children of the root node are 2, namely: 2 ,11
2
3(a) Given an array and a singly linked list, which of these two data structures uses more memory space
to store the same number of elements? Explain your answer. (2 marks)
(b) Consider the Java program below. Remember, a program is like an interesting poem, every word
and every line have their meanings. Then, answer the following questions:
public void some_function (int[] a)
{
Stack C =new Stack(a.length);
int[] b=new int[a.length];
for (int i=0;i<a.length;i++)
{
C.push(a[i]);
}
For (int i=0;i<a.length;i++)
{
b[i]=(int)(C.pop());
}
System.out.println("output :");
For (int i=0;i<b.length;i++)
{
System.out.println(b[i]);
}
}
i. Identify the data structures in the program. Name them, and state the size of each of the data
structures. (3 marks)
ii. Explain briefly, what you observe the program is doing. (3 marks)
iii. What is the worst-case time complexity for this algorithm? Explain briefly why. (2 marks)
Answer:
3(a) A singly linked list, because it also needs to maintain the pointers to the other nodes, which
takes up space. (2 marks)
(b)
i. The data structures in the program are: 1 stack, 2 arrays and theirs names are C, a, b
respectively. (½ mark each, Total = 1½ marks)
Size of the stack is given by the size of array a = a.length
Arrays a and b are of the same size (½ mark each, Total = 1½ marks)
ii. The program creates a stack C and fills it repeatedly- using the first for loop- with data items
in array a. Then, the data items in the stack are removed repeatedly and inserted into array b. At
the end the original items in array a are now arranged in reverse order in array b. Simply, the
program displays an output from array b that shows a reversal of original data in array a by
using stack C. (3 marks)
3
iii The worst case time complexity is determined by repetitions performed in each of the three
loops. The number of repetitions in each loop is determine by the size of the array a, since the
stack and array b are of the same size. Assuming the size of array a is n, then the number
repetitions in each of the three loops is n. Total time = 3n. As the coefficient 3 is not significant,
we can approximate the number of repetition to n. The time complexity for the algorithm clearly
is linear time. The bigger the size of array a (i.e., n), the longer the execution time of the
program. In Big O notation, this is O(n). (2 marks)
4(a) Mention four (4) types of linear and two (2) types of nonlinear data structures that you know (3
marks)
(b) Consider the algorithm below, and answer the following questions:
INPUT: A sequence of natural numbers of length n such that n >0
OUPUT: The largest and smallest element of S
1. max = S[0]; min = S[0]
2. for i = 1 to n do
3. if S[i] > max then
4. max = S[i]
5. else
6. if S[i] < min then
7. min = S[i]
8. end if
9. end if
10. end for
11. return max, min
4
How? It assumes that the largest and smallest element in an array is the element in the first position (i.e.,
S[0] ) and it then compares largest and smallest with all other values in the array. Largest changes to a new
value where such value is greater than Largest. The same logic applies in determining the smallest. (2 marks)
iii. The worst-case time complexity of the algorithm is determined by the n – the number of times the
loop will be executed. The bigger the value of n, the longer the execution time. Thus, this is a linear time
algorithm. In other words, the worst case time complexity of the algorithm is O(n).
(1½ mark for correct time complexity, 1½ mark for correct and brief explanation)
5(a) Given a 5 element stack S (from top to bottom: 6, 4, 2, 10, 8), and an empty queue Q, remove the
elements one-by-one from S and insert them into Q, then remove them one-by-one from Q and re-
insert them into S.
i. How does S look like (from top to bottom)? With the aid of well-labelled diagrams of
the stack and the queue, briefly show how you arrived at your answer. (3 marks)
ii. What is the time complexity for:
removing an element from the stack (1 mark)
inserting an element to the queue (1 mark)
(b) Fill in the following table the time complexities of these operations on an array of n items.
Use only big-O notation, and do not have any extraneous constants in your expressions.
Answer
i.
Original state of Stack Original state of Queue, Q
6
4 pop
2
10
8
6
4
2
10
8
5
6 4
2
10
8
6 4 2
10
8
6 4 2 10
6 4 2 10 8
(2 marks)
By deleting elements from the queue using FIFO, the stack is repopulated with the value now reversed
from the way it was originally.
S Q
8
10
2
4
6
(1 mark)
ii the time complexity for :
removing an element from the stack - O(1) (1 mark)
inserting an element to the queue – O(1) (1 mark)
6
b.
Operations Worst Case Average Case
Binary search of a sorted array O(log n) O(log n)
Access O(1) O(1)
deletion O(n) O(n)
Insertion O(n) O(n)
Heap sort O(n log n) O(n log n)
(2 marks)
7
5 6 0 1 7 8 2 3 4 9
5 0 6 1 7 8 2 3 4 9
5 0 1 6 7 8 2 3 4 9
5 0 1 6 7 2 8 3 4 9
5 0 1 6 7 2 3 8 4 9
5 0 1 6 7 2 3 4 8 9 End of second iteration
(1marks)
5 0 1 6 7 2 3 4 8 9
0 5 1 6 7 2 3 4 8 9
0 1 5 6 7 2 3 4 8 9
0 1 5 6 2 7 3 4 8 9
0 1 5 6 2 3 7 4 8 9
0 1 5 6 2 3 4 7 8 9 End 3rd iteration
(1 marks)
0 1 5 6 2 3 4 7 8 9
0 1 5 2 6 3 4 7 8 9
0 1 5 2 3 6 4 7 8 9
0 1 5 2 3 4 6 7 8 9 End 4th Iteration
(1 mark)
0 1 5 2 3 4 6 7 8 9
0 1 2 5 3 4 6 7 8 9
0 1 2 3 5 4 6 7 8 9
0 1 2 3 4 5 6 7 8 9 End of 5th Iteration
(1 mark)
7(a) Draw a simple diagram to describe briefly each of following data structures.
i. Linked list
ii. Stack
iii. Queue (6 marks)
(b) Given an empty queue Q, what does it look like after the following operations?
1. Q.enqueue(10)
2. Q.enqueue(5)
3. Q.dequeue()
4. Q.enqueue(7)
5. Q.enqueue(20)
6. Q.dequeue() (3 marks)
(c) Mention one (1) real life application of the stack data structures (1 marks)
8
Answers:
7(a)
i. A linked list is a linear data structure made up of nodes containing information as well as link (or
address of the) next information on the list. Information can inserted at any point in linked list.
Link to
the First
Node
Link
Null
Data Item
ii. Stack is a linear data structure in which the last item to be added to the stack is the first to be removed.
This implies that data items are added or removed from one point in the stack called TOP. The
operation for adding an item is a called a PUSH and to remove is POP.
PUSH 9 TOP
9
iii. Queue:
The queue is a First-In-First-Out data structure. This means the first item added to the queue comes out first.
Data items are added at the REAR and they are removed at the FRONT. A queue can be implemented in
programs using an array or a linked list.
FRONT REAR
A B C D E
(b) Given an empty queue Q, what does it look like after the following operations?
1. Q.enqueue(10)
2. Q.enqueue(5)
3. Q.dequeue()
4. Q.enqueue(7)
5. Q.enqueue(20)
6. Q.dequeue()
FRONT REAR
Empty Queue Q
10