Sample Exam 1 Solutions
Sample Exam 1 Solutions
There will be 6 basic questions, and 3 advanced questions, and two points per
question. So, the highest possible mark is 18 in total, of which 12 on the basic
questions and 6 on the advanced questions. Here is what you need to do to get each
grade:
● To pass the course, you must get 8 out of 12 on the basic questions.
● To get a four, you must get 9 out of 12 on the basic questions, and also get 2
out of 6 on the advanced questions.
● To get a five, you must get 10 out of 12 on the basic questions, and also get 4
out of 6 on the advanced questions.
Grade Total points Basic points Advanced
3 ≥ 8 ≥ 8 —
4 ≥ 11 ≥ 9 ≥ 2
5 ≥ 14 ≥ 10 ≥ 4
Good luck!
Write your anonymous code (not your name) here: ____________
The following algorithm takes as input an array, and returns the input array with
all the duplicate elements removed. For example, if the input array is {1, 3, 3, 2, 4,
2}, the algorithm returns {1, 3, 2, 4}.
1A) What is the order of growth of this algorithm, if the set S is
implemented using a red-black tree?
Order of growth: n
log n
(explanation, not needed to pass the question: S has size at most n, so the body of
the loop takes log n time, and it runs n times)
1B) What is the order of growth of this algorithm, if the set S is
implemented using a hash table with linear probing? You may assume
that the hash function used is of high quality.
Order of growth: n
(explanation: this time the loop body takes expected constant time)
Write your anonymous code (not your name) here: ____________
The binary search routine is almost finished, except for the blank spaces marked by
“_______”, which you should fill in. The idea of the routine is that, at each
point, if the key is present in the array at all then it can be found in the interval
array[low]...array[high].
Write your anonymous code (not your name) here: ____________
1 2 3 4 7 6 9 8 5
Write your anonymous code (not your name) here: ____________
4A) Draw the tree as a red-black BST. Draw black nodes as circles and
red nodes as squares.
Answer:
Write your anonymous code (not your name) here: ____________
4B) Insert 9 into the tree using the red-black insertion algorithm.
Write down the final tree.
Note: if you are a student of TDA416/DAT037, in the real exam there would be an
alternative question about AVL trees for you.
Answer:
(I solved this by inserting into the 2-3 tree and then converting to a red-black
tree. That works too!)
Write your anonymous code (not your name) here: ____________
0 1 2 3 4 5 6 7 8
9 18 12 3 14 4 21
In which order could the elements have been added to the hash table? There are
several correct orders, and you should give all of them. Assume that the hash table
has never been resized, and no elements have been deleted yet.
Answer: C, D.
Write your anonymous code (not your name) here: ____________
Node A C D B F G E H
Node A B D C E G F H
Node A D C F B G E H
Distance 0 1 2 3 3 6 8 9
Write your anonymous code (not your name) here: ____________
Answer: 0, 1, 3, 3, 3, 4, 5, 5, 6, 7
7B) What does the function f do? Explain as well the meaning of m.
Answer: It sorts the array (using counting sort). m is the number of buckets - the
input numbers must be all in the range 0 to m-1.
Write your anonymous code (not your name) here: ____________
Advanced question 8
Answer on a separate sheet of paper.
lookup(1) Returns 2
rlookup(2) Returns 4
The invariant is that the two maps always contain the same data. More precisely,
forward contains the mapping k → v, if and only if b
ack contains the mapping v →
k.
● new: set f
orward and b
ack to be empty red-black trees
● lookup(k): look up k in f orward using the BST lookup algorithm
● rlookup(v): look up v in b ack using the BST lookup algorithm
A) Work out how to implement delete, and write down your solution.
delete(k) {
if forward contains k {
v = forward.get(k)
forward.delete(k)
back.delete(v)
}
}
B) Work out how to implement put, and write down your solution.
This is rather tricky! The problem is that when adding k → v, there may already be a
mapping with key k, o r a mapping with value v. Both mappings have to be removed!
To get this right, I would recommend testing your code by hand, on the example
above, and checking that both maps have the right contents after each step.
put(k, v) {
if forward contains k {
v1 = forward.get(k)
forward.delete(k)
backward.delete(v1)
}
if back contains v {
k1 = back.get(v)
forward.delete(k1)
backward.delete(v)
}
forward.put(k, v)
back.put(v,k)
}
Write your anonymous code (not your name) here: ____________
For both parts, you should write down your answer either as pseudocode or Java
code. Your solution must take logarithmic time. Y
ou can use the standard
red-black tree operations (such as insertion and deletion) without explaining
how they are implemented.
Write your anonymous code (not your name) here: ____________
Advanced question 9
Answer on a separate sheet of paper.
In this question, you will think about d ouble-ended priority queues, a data structure
that represents a collection of (e.g.) integers and supports the following
operations:
9A)
Here is an idea for implementing a double-ended priority queue, which
unfortunately d oes not work:
Your job is to work out why this design does not work. Once you have done so, write
down a sequence of operations (calls to a
dd, d
eleteMin and d
eleteMax) which, if
we run them starting from an empty priority queue, give the wrong answer. You
should write down your answer in the following format (note that this example
gives the right answer, however):
Operation Right answer Actual answer
add(3) - -
add(5) - -
deleteMin() 3 3
deleteMax() 5 5
Answer: (you can test this by hand to see what goes wrong)
Operation Right answer Actual answer
add(3) - -
deleteMin() 3 3
add(2) - -
Write your anonymous code (not your name) here: ____________
deleteMax() 2 3
9B)
In a binary tree, the l evel of a node is defined as follows: the root of the tree has
level 0, its children are at level 1, its grandchildren are at level 2, and so on. In other
words, the level of a node is the distance from the node to the root.
Am
in-max heap is a complete binary tree satisfying the following invariant:
● If a node is at an e
ven level, its value is l ess than or equal to all values in the
node’s subtree.
● If a node is at an o
dd level, its value is greater than or equal to all values in the
node’s subtree.
Be careful: there are a number of special cases! Make sure that your algorithm also
works for heaps that contain only one item or only two items.
Hint: try drawing a min-max heap first, to get a feel for what the invariant means.
● If the root has two children, return the maximum of those children
● If the root has one child, return that child
● If the root has no child, return the root
(The observation is that the root’s children are at level 1, so each of them is the
maximum of its respective subtree. But note the special cases when the root
doesn’t have two children!)