You are on page 1of 6

STUDENT’S DECLARATION OF ORIGINALITY

By submitting this online assessment, I declare that this submitted work is free from all forms of
plagiarism and for all intents and purposes is my own properly derived work. I understand that I have
to bear the consequences if I fail to do so.

Final Online Assessment Submission

Course Code: BACS2063

Course Title: Data Structures & Algorithms

Signature: Ken

Name of Student: Ken Lee Kit Khen

Student ID: 21WMR05267

Programme: RSF G4

Date: 7 Oct 2021

Question 1

Question 2

Question 3

Question 4

TOTAL

Page 1 of 6
QUESTION 1

1a The most suitable Abstract Data Type (ADT) that can be applied in Beautiful Today
application is a stack. A stack will be chosen because stack is a linear collection of
entries in which entries may be only removed in the reverse order in which they are
added which follow a Last In First Out (LIFO) policy. The main page of the
application will show the latest 10 entries of the diary which is also a LIFO.
Assuming that the user wanted to view back the latest diaries, by using stack it
allows the application to perform a peek to the entries in the top of the stack. The
object stored in the stack will be the entries of the application which are the diaries
that wrote by the user. In each of the entries, there will be some variable to store
the content of the diaries which is the mood, date, text and images in the diaries.

1b

Page 2 of 6
QUESTION 2

2a The implementation and usage details are left out in an ADT specification is to
focus on the abstract properties without worrying about how it is going to be
implemented. So that, the user who is reading the ADT specification is able to
know more about the purpose, precondition, postcondition and the result that might
be returned from the function. Other than that, to abstract the ADT specification is
because it could enable the programmer to code the implementation and usage
details following their own preferrended practice which could make the
programmer easier.

2b T requeue()
Description: To remove the first entry of the queue and add the entry to the end of
the queue.
Precondition: The queue is not empty
Postcondition: The first entry is removed and added to the back of the queue
Return: The requeued entry from the queue

2c public T requeue() {
if (firstNode != null && lastNode != null) {
Node tempResult = firstNode;
firstNode = firstNode.next;
lastNode.next = tempResult;
lastNode = tempResult;
lastNode.next = null;
}
return tempResult.data;
}

2d The requeue method implemented using a fixed front array won’t be more efficient.
This is because when performing a requeue using a fixed front array it might
require to move the entries which are stored in the back of the queue one step
forward by using a loop. The Big-O notation for it is an O(n) which is not efficient
compared to the O(0) that obtained from the linked queue.

QUESTION 3

3a The overall purpose of the method is to return the reversed string for the input
string from the argument which does not contain any duplicated alphabet. The
method will firstly check and remove all the duplicated alphabet by comparing in
line 6 and reverse the remaining alphabet by pushing out from the stack which is
shown in line 8.

Page 3 of 6
3b (i) HTILN

3b (ii)

3c (i)
5 10 15 20 25 30

For example if it is required to search for the value 30 in the set of elements shown
above by a binary search.
Firstly, we are required to find the midpoint of the element by breaking it into two.
Mid = (0+5) /2 = 2. The value at index 2 is 15.
Since the target is 30 > 15 search at the right subarray [3..5]

5 10 15 20 25 30

Secondly, find the midpoint of the right subarray [3..5] by breaking it into two.
Mid = (3+5) /2 = 4. The value at index 4 is 25.
Since the target is 30 > 25 search at the right subarray [5..5]

5 10 15 20 25 30

Secondly, find the midpoint of the right subarray [3..5] by breaking it into two.
Mid = (5+5) /2 = 5. The value at index 5 is 30.
Therefore the target value has been located at index 5

3c (ii) The best case for the Binary Search is O(1) because the best case for the binary
search is to locate the target in only one search. For example, from the six
elements in 3c(i), if the target value is 15 which is the midpoint of the element, the
binary search only requires searching one time to obtain the targeted value.

eg.
Mid = (0+5) /2 = 2. The value at index 2 is 15.
Therefore the target value has been located at index 2

Page 4 of 6
QUESTION 4

4a To redraw the binary search tree after the ‘H’ There might be 2 different way to
redraw the binary tree.

The first possibility for the redraw is to replace the ‘H’ with a ‘F’. Which is because,
from the left subtree of ‘H’ the biggest node will be chosen which biggest in H
node’s left subtree is the ‘F’ node, so ‘F’ is chosen to replace H node.

The second possibility for the redraw is to replace the ‘H’ with a ‘K’. Which is
because, from the right subtree of ‘H’ the smallest node will be chosen which
smallest in H node’s right subtree is the ‘K’ node, so ‘K’ is chosen to replace H
node.

4b (i) Method 1 is not suitable to compute the hash code for Strings because it will cause
a collision. This is because by adding up each character the collision will occur
when the string contains the same amount of specific character but with different
order. For example the value for “ABC” and “BAC” are the same which is both (65
+ 66 + 67 = 198)

Page 5 of 6
4b (ii) To compute the hash code for the credit card number and convert it to a hash
index, we need to add up the number 4 by 4 together and then modulo the result
which is added up with the hash table size which is a 2003.
Eg:
First added up the number 4 by 4
2222 + 4107 + 4036 + 0010 = 10375
Modulo the result with 2003
10375%2003 = 360

The hash index will be 360 after the computation.

4c The real-life example for Dictionary ADT is an emergency call directory. For
example, we call 999 for every emergency call which could pass the call to the
police station, hospital, fire station, etc. By using the terms of hashing we could
said that the 999 is the hash key for the call directory and that the directory to the
respective emergency hotline is the hash value for the dictionary.So, The object
would be stored in the key will the first 3 number which is a ‘999’ and the value will
be directory hotline number for the respective emergency hotline (eg. 110 for
hospital hotline).

Page 6 of 6

You might also like