17CSCI01C Data Structures Unseen Exam
17CSCI01C Data Structures Unseen Exam
Confirmation Sheet
Proof-reader comments
-
Spelling and Grammar -
-
-
-
Template -
-
-
-
Marking schema -
-
-
-
ILOs matching -
-
-
Any Other Comments -
-
-
-
Date of examination
12 page answer
book
Graph paper
Other
[Adjust to your exam by replacing items in bold, and remove this line]
Instructions to students:
front
before next before next before next before next
A B C D
[5 marks]
b) What is the execution time required to delete the last element in a
circular linked list of length N. Assume P points to the last node in
the list.
i. O(N)
ii. O(log2 N)
iii. O(1)
iv. O(2)
[5 marks]
By using the Depth First Search algorithm which of the following is true?
i. A, B, E, G, I , J, C, F, H, D.
ii. A, E, B, I, J, F, H,G, C, D.
iii. A, G, F, H, J, D, B, C, E, I.
iv. A, H, I, J, E, G, F, E, C, B.
a) O(n)
b) O(n2)
c) O(n3)
d) O(n5)
b) Sum = 0;
for(i=1; i<N; i++)
for(j=1;j<i*i; j++)
Sum++;
e) O(n)
f) O(nlog2 n)
g) O(n3)
h) O(n2)
c) Count=0;
for (i=1; i<log2 n; i++)
for (j=1; j<log2 n; j++)
Count++;
i) O(log2 n2)
j) O(log2 n)
k) O(log2 n)
l) O(n2)
Find the hash values to insert each of the following items in the hash
table{26 ,70 , 18 , 31, 54 ,93 }.
Use the following hash function:
Equipment allowed
“None”
[Adjust to your exam by replacing items in bold, and remove this line]
Instructions to students:
no.
front
before next before next before next before next
A B C D
[5 marks]
iii. What is the execution time required to delete the last element in a
circular linked list of length N. Assume P points to the last node in
the list.
v. O(N)
vi. O(log2 N)
vii. O(1)
viii. O(2)
[5 marks]
Page 1 of 13
Q2
a) What is the output of the following segment of code:
stackType <int> stack;
int x, y;
x = 4;
y = 0;
stack.push (7);
stack.push (x);
stack.push(x+5);
y= stack.top( );
stack.pop( );
stack.push(x+y);
stack.push(y-2);
stack.push(3);
x = stack.top( );
stack.pop( );
cout << “x = ” << x << endl;
cout << “y = ” << y << endl;
while (!stack.isEmptyStack( ))
{
cout << stack.top ( ) << endl;
stack.pop ( );
}
[5 marks]
b) Show the steps to convert the arithmetic expression a+ b∗c−d from the
infix notation to the postfix notation using a stack.
[15 marks]
[Q1 Total: 20 marks]
Answer
a) x = 3 [1 marks]
y=9 [1 marks]
7 [1 marks]
13 [1 marks]
4 [0.5 marks]
7 [0.5 marks]
Page 2 of 13
b)
1) Initially the Stack is empty [0.25 marks] and the output postfix string is
empty [0.25 marks]. Now, the first character read from the expression is
'a' [0.25 marks]. Since it is character not an operator it is put to the output
postfix string [0.25 marks]. The next input character is '+' [0.25
marks]and since it is an operator so it is pushed onto the stack [0.25
marks].
[0.5 marks]
Next input character is 'b' [0.25 marks] so it will be put to the postfix string
[0.25 marks]. The next input character is '*' which is an operator [0.25
marks] so it is pushed to the top of the stack [0.25 marks] because '+' has
lower precedence than '*' [0.25 marks].
[1 mark]
The next input character is 'c' [0.25 marks] which is placed in the output
postfix string [0.25 marks]]. Next character input is '-[0.25 marks] and
since it is an operator [0.25 marks] its precedence is compared with that of
the topmost operator on the stack which is '*' [0.25 marks] and has a higher
precedence than '-' [0.25 marks]. So '*' will be popped out from the stack
[0.25 marks] and added to the output postfix string [0.25 marks]. Now, the
topmost element of the stack is '+' [0.25 marks] which has equal priority to
'-' [0.25 marks]. So, pop the '+' from the stack [0.25 marks] and add it to
the output postfix string [0.25 marks]. Finally, the '-' will be pushed to the
stack [0.25 marks].
Page 3 of 13
[2 marks]
Next input character is 'd' [0.25 marks] which is added to the output postfix
string [0.25 marks]. Now all characters have been scanned [0.25 marks]
so we must pop the remaining elements from the stack [0.25 marks] and
add it to the Postfix string [0.25 marks]. At this stage we have only '-' [0.25
marks] in the stack so it is popped out [0.25 marks] and added to the
Postfix string [0.25 marks]. Now all characters are scanned and the stack
and output postfix string looks like [0.25 marks]:
[4 marks]
Q 3 Assume the following linked list where each node consists of two fields the
data field “info” field and the address field “link”. The “info” field stores
integers and the “link” field stores pointers.
Page 4 of 13
ii. Delete the node with 23, and deallocate the memory occupied by this
node.
iii. Make J point to the last node in the list.
[Q2 Total: 20 marks, 10
each]
Answer
a)
i. True
ii. True
iii. False
iv. False
v. True
[2 marks for each item]
b)
2) p = A -> link; [1
mark]
delete p; [2
marks]
Page 5 of 13
Q 4 Answer the following questions:
a) Compare between tree and graph data structures. What is each one is
useful for? [12 marks]
b) Assume the following precedence graph.
a) All tree structures are hierarchical by nature [0.5 marks] which means that
each node has only one parent node [0.5 marks]. Trees are useful in storing
data that have the hierarchy properties such as family tree or file systems
[1 mark]. There are three possible traversal methods for a tree namely, preorder
[0.5 marks], inorder [0.5 marks] and postorder [0.5 marks] methods. Some
data has no hierarchical property which means that a node can have more the
one parent or there are more connections between nodes which do not fit into a
hierarchy structure. Graph data structures can be useful in these situations [0.5
marks]. A graph, like a tree, is a collection of nodes and edges [0.5 marks], but
has no rules dictating the connection among the nodes [0.5 marks]. A graph
consists of a number of data nodes [0.5 marks], each of which is
Page 6 of 13
called a vertex [0.5 marks]. Any vertex may be connected to any other [0.5
marks], and these connections are called edges [0.5 marks]. A tree data
structure can be described as a connected [0.5 marks], acyclic graph [0.5
marks] with one node designated as the root node [0.5 marks]. It is acyclic
because there are no paths in a tree which start and end at the same node [0.5
marks]. A graph is cyclic if it contains paths that start and end at the same node
[1 mark]. There are two possible traversal methods for a graph namely, breadth-
first [0.5 marks] and depth-first [0.5 marks] methods. Graphs are very useful in
representing data networks such as: computer networks, maps of cities and
traffic roads [1 mark].
b)
ii. From the dependency lists it is clear that S1 and S2 can be executed in
parallel [0.5 marks] because they do not wait any results from each other
[0.5 marks].
Page 7 of 13
After S4 finished execution S5 & S6 can be executed in parallel as no one
depends on the results of the other [0.5 marks].
Page 8 of 13
Q 5 Answer the following question:
b) What is a hash table or hash map?
[5 marks]
d) Suppose you have the following hash table:
0 1 23 4 5 6 7 8 9
Find the hash values to insert each of the following items in the hash table
{26 ,70 , 18 , 31, 54 ,93 }.
Use the following hash function:
Hash value = key value % number of slots in the hash table
[5 marks]
e) Suppose the hash table contains the following items
0 1 23 4 5 6 7 8 9
70 31 93 54 26 18
Use the linear probing method to resolve the conllision in the hash table
that occurs when the next item to insert is 40 and show the contents of
the hash table. [10 marks]
[Q4 Total: 20
marks]
Answer
a)
Hash table or hash map is a data structure used to store key-value pairs.
[1 mark]
It is a collection of items stored to make it easy to find them later.
[1 mark]
It uses a hash function to compute an index into an array of buckets or slots
from which the desired value can be found. [1 mark]
It is an array of list where each list is known as bucket. [1 mark]
It contains value based on the key. [1 mark]
Page 9 of 13
b)
Data Item Value % No. of Slots Hash Value
26 26 % 10 = 6 6 [1 mark]
70 70 % 10 = 0 0 [1 mark]
18 18 % 10 = 8 8 [1 mark]
31 31 % 10 = 1 1 [1 mark]
54 54 % 10 = 4 4 [0.5 marks]
93 93 % 10 = 3 3 [0.5 marks]
0 1 23 4 5 6 7 8 9
70 31 40 93 54 26 18
[1 mark]
Page 10 of 13
Q 6 Answer the following question
a) Write the code for the recursive function f (n)=n∗f (n−1), base condition:
if n≤1 then f (n)=1.
b) What is the time complexity of the hash table in the average case? What
is the time complexity if you have N elements in one bucket? Justify your answer
for each case scenario.
Answer
a)
using namespace std; [0.5 marks]
int f(int n) [0.5 marks]
{ [0.5 marks]
if (n <= 1) [0.5 marks]
return 1; [1 mark]
else [0.5 marks]
return n*f(n-1); [2 marks]
} [0.5 marks]
int main() [0.5 marks]
{ [0.5 marks]
int num; [0.5 marks]
cout<<"Enter a number: "; [0.5 marks]
cin>>num; [0.5 marks]
cout<<"Output:"<<f(num); [0.5 marks]
return 0; [0.5 marks]
} [0.5 marks]
b) In the average case the time complexity is O(1). [1 mark] This is because if
a good hash function [1 mark] is chosen it is very rare that many items will
be hashed to the same key [2 marks]. In the worst case [1 mark], you could
be inserting N elements [1 mark] all of which hash to the same bucket
[1 mark]. Then, for this data set, deletion and search will also be O ( n ) [1
mark], this is because it too elements were hashed in to the same key this
key may take O(n) times [2 marks].
[Q5 Total: 20 marks, 10 each]
Page 11 of 13
Page 12 of 13
Module Specification – (Programme Specs Ver. 4.2) 2017/18
Aims
The aim of the module is to introduce the concepts of data structures and algorithm design and
provide a foundation for advanced studies in computer science. The main focus is on the use of
data structures and abstraction methods, both in terms of data representation and algorithmic
access.
1. Recognize fundamental data structures and concepts of algorithms complexity analysis in line
with the principles of good software design. [ A1 ]
2. Select the most appropriate data structure and algorithm for a given problem.[B1]
3. Develop an analytical approach to interpreting problem specifications and a systematic
approach to problem solving. [B2]
Transferable skills
5. Search for and retrieve needed information from programming language libraries and
documentation. [D1]
Employability
This module will provide opportunities for students to:
1. Understand the importance of maintaining performance under pressure. [A3]
2. Develop analytical and systematic problem solving skills. [B.1.2]
3. Carry out a range of complex ICT activities related to their work that involve application
software. [B.3.1]
Page 13 of 13
Indicative Content
Aggregate Data Structures: strings, arrays, abstract data types (structures and classes)
Pointers and dynamic memory.
Stack abstraction and implementation.
Queue abstraction and implementation.
Linked lists abstraction and implementation.
Trees: Binary Trees abstraction, traversals, and algorithms
Heaps and priority queues
Hashing and hash tables
Introduction to algorithms, algorithms complexity, and time/space complexities.
Sorting and searching algorithms: insertion sort, linear and binary search, and their
complexity analysis.
Tutorial - - - -
Laboratory 2-5 12 2 24
Assessment
Methods of Feedback
In response to assessed work:
Specific feedback will be provided after lab tests through marking criteria and in written form
as appropriate for each student. If students require additional feedback, they are welcome to
speak with the TA, or module leader.
Generic feedback for unseen exam on e-learning.
Page 14 of 13
Dialogue between students and staff in workshops and Labs.
Page 15 of 13