Final Exam
Final Exam
Give one copy of this examination booklet to each student, and also enough lined
writing paper for answers. Students should not be allowed to bring any notes, books
or handouts into the exam. Collect the answer papers only, checking that the students
have written their name and ID. The students may keep this question paper.
INSTRUCTIONS TO STUDENTS:
Make sure that your exam paper contains 14 pages and 7 questions.
Each question is worth 25 marks. Do not answer all 7 questions. Answer any 4 of
the questions. You should first spend around 20 minutes reading through all of the
questions and deciding which ones you plan to answer. Then aim to spend around 40
minutes on each question.
Write your answers neatly on the separate paper provided, clearly numbering your
answers with the appropriate question numbers. Also write your name and ID number
at the top of each page. You may not use any notes, handouts or textbooks. The
maximum score is 100. Your mark from this examination will make up 50% of your
final mark for this course.
After you have finished, hand your answer sheets only to the invigilator. You may
keep this examination booklet. If you leave the examination early please leave quietly
and try not to disturb other students.
Page 1 of 14
Question 1 - Complexity Analysis (25 marks)
a) Briefly explain the meaning of the following (you do not need to give the formal definition):
i. Ω-notation
ii. θ-notation
(2 marks)
(4 marks)
d) Two algorithms have been developed to solve a particular problem. Algorithm 1 requires 7n2+n
assignment statements, and algorithm 2 requires 109n – 4 assignment statements, where n is the
size of the input data.
ii. If it is guaranteed that the size of the input data will never exceed 1 million, which
algorithm is the most efficient?
iii. Which complexity notation, apart from big-O, is appropriate for describing the
complexity of algorithm 2?
e) State the big-O complexity of sequential search in the best, average and worst cases.
(3 marks)
f) For each of the following pieces of code, state and prove the big-O complexity in terms of the
size of the data n (consider assignment statements only). Remember that to prove the
complexity you must find values for the constants c and N that satisfy the formal mathematical
definition.
i. sum = 0;
for (i = 0; i < n; i++)
sum++;
Page 2 of 14
ii. x = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
x += i * j;
Page 3 of 14
Question 2 - Linked Lists (25 marks)
Below is an outline of a C++ class for a singly-linked list of integers. Examine the code and answer
the questions that follow.
class IntSLLNode {
public:
int info;
IntSLLNode *next;
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
};
class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
void deleteNode(int);
bool isInList(int) const;
void printAll() const;
private:
IntSLLNode *head, *tail;
};
a) Explain, using a diagram, what is meant by a linked list in computer programming. What are the
advantages of linked lists over arrays?
(6 marks)
b) Explain the difference between a singly-linked list and a doubly-linked list. Again, use a
diagram as well as giving a text explanation. What is the advantage of using a doubly-linked
list?
(4 marks)
Page 4 of 14
c) Explain the advantage of including two access pointers (head and tail) for the IntSLList
class.
(2 marks)
d) Write the function body for the addToTail() member function for the IntSLList class.
The function should add a new node at the end of the list.
(5 marks)
e) State the big-O complexity of the addToTail() operation. Justify your answer.
(3 marks)
f) Write a member function to check whether two IntSLList objects have the same contents.
Because the new function is a member function of IntSLList it only needs to take one
argument (another IntSLList object), and return a true or false value. It should return true if
the two lists are identical, and false otherwise.
(5 marks)
Page 5 of 14
Question 3 - Stacks and Queues (25 marks)
a) Briefly describe the operation of the stack and queue data structures. What is the difference
between them? For each, name the five operations that are needed to properly manage the data
structure, and name one programming application in which they would be useful.
(9 marks)
b) What do LIFO and FIFO stand for? Which one describes the operation of a stack and which one
describes a queue?
(4 marks)
c) Show how a stack would appear after each of the operations listed below (i.e. draw a diagram
showing the contents of the stack after each operation):
(3 marks)
d) Show how a queue would appear after each of the operations listed below (i.e. draw a diagram
showing the contents of the queue after each operation):
(3 marks)
e) What is a deque? Describe the operation of a deque, and explain the differences and similarities
between deques and stacks/queues.
(4 marks)
(2 marks)
Page 6 of 14
Question 4 – Recursion (25 marks)
a) Explain the advantages and disadvantages of using recursion in computer programming. What
factors would you consider when deciding between a recursive and an iterative implementation?
(4 marks)
b) Explain the meaning of the terms tail recursion and nontail recursion. Give one simple example
of each. Which of the two is more easily converted into an iterative equivalent?
(5 marks)
c) What is the purpose of the run-time stack in computer programming? Describe, using a diagram,
the operation of the run-time stack. Clearly indicate what information is typically stored on the
run-time stack, and show how the run-time stack handles recursion.
(8 marks)
d) Briefly explain what would be the output produced by the following recursive function.
void f(int n) {
if (n > 0) {
cout << n << “, “;
f (n – 1);
}
}
(2 marks)
Page 7 of 14
e) The Fibonacci sequence is a series of integers, such that the first and second numbers in the
series are 0 and 1, and any subsequent number is calculated by summing the previous two
numbers in the series. For example, the series starts with 0, 1, 1, 2, 3, 5, 8, 13, etc. The
following is a recursive implementation of a function that computes numbers in the Fibonacci
sequence.
(2 marks)
ii. Do you think recursion is a good choice of implementation in this case? Explain your
answer.
(4 marks)
Page 8 of 14
Question 5 - Trees and Graphs (25 marks)
Examine the trees illustrated below, and answer the questions following.
(1 mark)
(1 mark)
(1 mark)
(1 mark)
Page 9 of 14
e) Which of the trees shown above are binary search trees?
(1 mark)
f) Write down the order in which the nodes of tree (f) above would be visited in preorder, inorder
and postorder depth-first traversals.
(3 marks)
g) Draw the trees that would result if the 5 node from tree (f) were deleted by the deletion by
merging and deletion by copying techniques.
(4 marks)
h) Indicate how the following max heap would be stored if an array implementation were used (i.e.
draw the array and show what value each array element contains)
(4 marks)
i) Explain the meaning of a rotation in a binary tree. Draw a diagram to show how the positions of
nodes change when a node is rotated. With what type of binary tree is the rotation operation
commonly used?
(4 marks)
Page 10 of 14
Examine the graphs shown below, and answer the questions following.
(1 mark)
(1 mark)
(1 mark)
(1 mark)
(1 mark)
Page 11 of 14
Question 6 – Sorting (25 marks)
a) Explain the operation of the selection sort algorithm. Use the following array to illustrate your
answer.
8 3 10 7 1 12 4 15
(4 marks)
b) What is the big-O complexity of selection sort for data movements and comparisons in the best,
average and worst cases?
(3 marks)
(4 marks)
d) Illustrate how the LSD Radix sort algorithm would sort the following list:
The algorithm should use 3 passes - you should draw pictures showing what the ordering of the
array is after each pass.
(6 marks)
e) What is the big-O complexity of radix sort in the best, average and worst cases? What limitation
of the algorithm makes it sometimes difficult to use?
(2 marks)
f) Describe the operation of the Quicksort algorithm. Use the list given below to illustrate your
answer.
1 4 3 9 6 7 8 5 2
(4 marks)
g) Explain the importance of choosing the right bound in Quicksort. Describe 2 common
techniques for selecting the bound.
(2 marks)
Page 12 of 14
Question 7 – Hashing (25 marks)
a) Hashing aims to reduce the complexity of search operations to O(1). Briefly explain how it can
achieve this. Under what circumstances does it achieve this aim?
(3 marks)
b) When do collisions occur in hashing? What effect do collisions have on the expected search
time for data items in the hash table?
(2 marks)
i. Mid-square function
iii. Extraction
Key: 13 5 26 3 14 25 10
Value: A B C D E F G
Describe how these key-value pairs would be added to a hash table of size 10, using a simple
division hash function (i.e. h(K) = K modulo 10), that uses each of the collision resolution
techniques listed below. Show clearly, using a diagram, what the final state of the hash table
would be in each case.
i. Linear probing
(3 marks)
Page 13 of 14
f) Both linear probing and quadratic probing are open addressing techniques. Briefly explain what
open addressing means, and state one problem that can occur with such techniques. Briefly
describe one approach to overcoming this problem.
(5 marks)
END OF EXAMINATION
Page 14 of 14