TDDC32 Tryout Exam

advertisement
TDDC32 Tryout Exam
Rules:
•
•
•
•
•
•
No materials allowed
A total of 21 points can be opbtained. For a grade of 3 the number of points required is
11. For a grade of 4 it’s 14 and for a grade of 5 it’s 17.
For every assignment you get 3 points. If there are 2 subassinments per assignment you
get 1.5 points per subassinment. If there are 3 subassinments then you get 1 point per
subassinment.
Note that the number of assignments and the points per assignment may change in the
real exam.
The problem 7 is to be added .
Happy exercising
Assignments:
1) OOAD principles:
1. Describe 2 principles of OOAD
Abstraction. Means to identify essential characteristics. It's a principle that is helping to make
complicated things simple. Applying abstraction on the design of data structures we obtain abstract
data types (ADTs). An ADT specifies what operations are supported not how they are supported. In
Java an ADT can be expressed by an interface. Classes, that specify how the operations are realised,
are used to implement the ADTs.
Hierarchy. Refers to the possibility to organise software modules by their abstraction levels, starting
from the more abstract levels and progressing towards towards more specific. It improves reusability
as the more specific levels can inherit common behaviour from the more abstract levels (t.e. a sedan, a
truck and a SUV are all automobiles). It also helps simplifying the original problem by allowing a more
abstract design in the beginning.
2. What is a method signature and how does it relate to polymorphism?
A method signature is the unique combination of the name, the number of arguments and their type that
defines a method. Polymorphism (having many forms) of methods can be either by overriding or by
overloading. By overriding we have a method with the same signature in a subclass replacing a
method in the superclass. Thus the subclass will behave differently from the superclass for the same
function. In the case of overloading methods with different signatures but the same name can be
applied on different types of arguments and perform similar operations. For instance the function
add(Integer a, Integer b) and add(Double a, Double b).
2) UML
1. What are use case diagrams and where are they used?
A use case diagram displays the relationship among actors (roles of the users) and use cases (set of
usage scenarios). The are helpful in exposing requirements and planning the project. During the initial
stage of a project most use cases should be defined, but as the project continues more might become
visible.
2. What is the similarity and difference between a sequence diagram and a collaboration
diagram
Interaction diagrams model the use cases by detailing how operations are carried out -- what messages
are sent and when. Sequence diagrams are organized according to time. Collaboration diagrams show
the same information, however the focus is not put on the time at which the operations are carried out,
but on the object roles (with respect to the operations).
3) AVL trees
1. Construct an AVL tree, when the following values, arriving in the following order: 9 6 7
2 1 11 10 3 8 4 5:
9
double rot.
left-right
6
7
7
6
6
9
7
9
single rot.
right
2
7
9
2
1
6
1
9
2
1
7
7
7
1
11
6
6
3
10
2
6
5
9
10
2
1
11
4
3
8
10
4
3
9
9
6
11
8
5
4
7
1
double rot.
left-right
10
2
double rot.
right-left
single rot.
left
11
8
2. What is a FIBONACCI tree and why is it important?
A Fibonacci has the highest height to number of nodes ratio (is a the “worstly balanced” AVL tree
compared to a perfect balanced tree). It is built Fn = Fn-1 + • + Fn-2. It can be shown that the height of
the Fibonacci tree cannot be larger than twice the height of a perfectly balanced tree with the sae
number of nodes. Thus all the operations on the balanced tree are guaranteed to have complexity
O(logn).
4) Splay trees:
1. What is the advantage of using splay trees over AVL trees?
Splay trees are simpler to implement, as there is no height information to be stored and checked, or
rebalancing to be preformed. For situations where multiple reads are performed on a small number of
nodes (i.e. 10% of a program is run 90% of the time), the access is very fast as the most frequent nodes
are splayed up / near to the root. At the same time while guaranteeing an amortised running time for the
three base operations that is O(logn), that is on the same level as AVL trees.
2. Show, step by step how a removal of the value 5 occurs in the following splay tree:
1
2
7
6
5
3
4
First we find the value that has to be removed. Then we remove the value like for any binary tree. I.e.
we fin the smallest value in the left subtree of 5 and replace 5 with it (in this case 4). Then we delete
the node where 4 was stored and splay its parent ( node 3 !) up to the root. This is a sequence of zigzig, zig-zag, and zig movements, as showed below:
1
1
2
1
2
7
6
4
3
2
7
6
4
3
1
3
zig-zag
7
zig-zig
3
1
3
zig
2
7
2
4
6
4
4
7
6
6
x
5) Hashtables
1. Why/when does one want to increase the size of a hashtable?
We want to increase the size of a hashtable so that the number of probes is not increasing too much.
For example in open addressing clusters will build up, and will increase the number of probes (when
traversing the clusters). In separate chaining, the probes increase with the length of the chain. However,
in separate chaining there are no clusters, so the typical load factors that trigger a hash table increase
and a rehashing are 0.5 for open addressing and 1 for separate chaining.
2. For a hashtable of size 11 the primary hash function is h(k) = k % 11. Choose a dual hash
function d(k), explain your choice if different from the standard one, and insert: 11 76 22
44 91 84 47 75 14 55. Show the final hashtable, h(k), d(k), and the probed locations in
the hashtable for all the values to be inserted.
The dual function d(k) = 7 - (k % 7). Thus hi = (h(k) + d(k) * i) % 11, where i = 0, 1, 2,…
index value h(k)
0 11
0
1 14
3
2 75
9
3 91
3
4 55
0
5 44
0
6 22
0
7 84
7
8
9 47
9
10 76
10
d(k) probes:
0
7
3,10,6,2,9,5,1
2
9,0,2
3
1
0,1,2,3,4
5
0,5
6
0,6
7
9
10
6) Skip Lists & Heaps
1. What is the advantage of skip lists over lists implemented with a) arrays b) linked lists with
respect to find() and insert()?
The complexity of operations on a sorted array list: find O(log n), insert O(n), remove O(n). The
complexity of a sorted linked list: find O(n), insert O(n) that is O(1) after a find, remove O(n) that is
O(1) after a find. The complexity of find() for a skip list is in average O(logn), and since the skip list is
a sorted linked list, insert will be also O(logn) and remove O(logn).
2. How many children can a node have in a heap and what is the heap order property?
In a heap every node can have at most 2 children and the heap order property says that for every node
the key is greater or equal to the key stored at its parent (if a parent exists).
7) (a,b) Trees
1. Show what happens step by step when you remove the values 20, 3 from the following (2,5)
tree.
20
10
3
6
30
18
23 26
27
40
50
33 39
42
44
53
56
59
Preliminary notes. A thing to not is that for an (a,b) tree the minimum number of keys in a node is a-1
and the maximum b-1. In our case the minimum is 1 and the maximum 4. Also, the root node is the
only one that can have between 2 an b children (corresponding to - between 1 and 4 keys).
Now we delete 20. Always when we delete keys from an inner node, we first search the largest key in
the left subtree of that key (or the smallest in the right subtree), and we replace the value of the key in
the inner node with the leaf value and delete the key in the leaf:
18
10
3
30
6
23 26
27
40
50
33 39
42 44
53 56
59
As the leaf node got less than the minimum number of keys (underflow) we first look for a transfer
from one of its siblings. In this case we have only a left sibling and the transfer is possible since it has
more than a-1 keys. In the lending priocess the parent key is also involved (think as a shift of keys
involving the parent) and a new key will be promoted (move up) in the parent node:
18
6
3
30 40
10
23 26
27
50
33 39
42 44
53
56
59
Now we delete key 3. As we have an underflow, and the sibling is at its minimum number of keys, we
have to do a fusion, fusing the node with a sibling node. In this process the parent key will be demoted
(moved down). If we have an underflow in the parent node the process will continue.
18
30 40
6
10
23 26
27
50
33 39
42 44
53
56
59
As we have an underflow at the parent node we first try a transfer which is possible in this case:
30
18
6
10
40
23
26
27
50
33 39
42
44
53
56
59
2. Show what happens step by step when you add the values 62, 63, 24, 25 to the following
(2,5) tree.
20
10
3
6
30
18
23 26
27
40
50
33 39
42
44
53
56
59
We add 62 and the number of keys in the last leaf becomes 4, which is ok. Then we add 63 and create
an overflow in the last leaf node:
20
10
3
6
30 40
18
23
26
27
33
50
39
42
44
53
56
59
62
63
Since we have an overflow, we split the node and promote the median key to the parent. The parent
does not become overflown:
20
10
3
6
30
18
23
26
27
40
33
50 59
39
42 44
53
56
62 63
42
53 56
62 63
Now we add 24 and 25 and the same happens to the leaf node:
20
10
3
6
25
18
23
24
26
27
30
40
33
50
39
Now the parent node has become overflown and we have to split it:
59
44
20
40
10
3
6
25
18
23
24
26
27
30
50
33
39
59
42
44
53 56
62 63
3. Show that when you have a split of an (inner) node in an (a, b) tree, the number of children
of any of the 2 resulting nodes is larger than the minimum number of children allowed.
A node is splitted if it has at least b+1 children. As we promote he median key, the smaller of the nodes
resulting in the split will have the number of children, denoted here as c as follows. If b is odd then c =
(b+1)/2. If b is even then c = b / 2. From the definition of (a,b) trees we know that a ≤ (b+1)/2. Now if b
is odd then it’s immediately visible that c ≥ a. If b is even, then a ≤ b/2, so again c ≥ a.
q.e.d.
Download