Cst0 Computer Science Tripos Part I
Cst0 Computer Science Tripos Part I
Cst0 Computer Science Tripos Part I
CST0
COMPUTER SCIENCE TRIPOS Part IA
Answer one question from each of Sections A, B and C, and two questions from
Section D.
Submit each question answer in a separate PDF. As the file name, use your
candidate number, paper and question number (e.g., 1234A-p1-q6.pdf). Also write
your candidate number, paper and question number at the start of each PDF.
SECTION A
Sequences (lazy lists) and trees are fundamental types in functional programming.
Here are definitions of sequences and trees with integer elements:
(ii ) Define sequences s1 and s2 for which equal_seq s1 s2 does not terminate.
[3 marks]
(c) The fringe of a tree is the left-to-right sequence of the values at the leaves.
For example, the fringe of Branch (Leaf 3, Branch (Leaf 10, Leaf 4)) is the
sequence 3, 10, 4.
(i ) Define a function fringe that computes the fringe of a tree. Your function
should have the following type:
[5 marks]
(ii ) Using the functions you have defined above or otherwise, write a function
equal_fringes that determines whether two trees have equal fringes.
[2 marks]
2
CST0.2021.1.3
Your answers may use the List module and assume this functional array code:
3
CST0.2021.1.4
SECTION B
3 Object-Oriented Programming
A programmer is using a cut-down version of Java that does not support static fields
or inheritance (neither extends nor implements). Static methods and static inner
classes are still supported.
(a) What are the implications of this for static methods in terms of:
(c) What are the drawbacks and benefits of your scheme compared to static fields?
[4 marks]
4
CST0.2021.1.5
4 Object-Oriented Programming
A program which decrypts files under the Swap Encryption Scheme by swapping
pairs of characters is given below. Some code has been omitted and you do not need
to understand the operation of the algorithm.
(b) The program attempts to use Swapper as part of the Decorator pattern. What
changes would you make to improve the design? [2 marks]
(c) Explain how this program demonstrates the open-closed principle. [2 marks]
(d ) How would you change this implementation to allow users to specify an arbitrary
operation to apply to pairs of characters (rather than just swapping them)?
[4 marks]
(e) Explain why this design does not satisfy the open/closed principle with respect
to adding support for decrypting images. What are the implications of this for
object-oriented program design? [4 marks]
5
CST0.2021.1.6
SECTION C
5 Introduction to Probability
(a) A travel agency is surveying their customer satisfaction by randomly polling 300
of their customers. From experience, 80% of their customers are typically happy
with their service. Let H be the number of happy customers in the current poll.
(iii ) Using the approximation from Part (a)(ii ), what is the probability that
more than 220 and fewer that 260 customers are happy in the current poll?
[4 marks]
(iv ) Now, let X be the proportion of customers that are happy in the current
poll. Following your approximation from Part (a)(ii ), give the distribution
for X, including its parameters, expected value and variance. [3 marks]
6
CST0.2021.1.7
6 Introduction to Probability
(a) A korfball player is practicing shots and has a 90% chance of scoring. Assume
that their shots are independent of one another.
(ii ) Following the experiment in Part (a)(i ), let M be the number of shots
made before the first miss. Specify a suitable distribution for M including
its parameters, and compute the expected value and variance. What is the
probability of M > 100? [4 marks]
(iii ) Use a suitable distribution to approximate the probability that there are
at most 3 misses in the first 200 shots. Note: you do not need to compute
the final numerical value. [3 marks]
(iii ) Suppose now that n is an unknown parameter and you observe the absolute
difference between the labels of the first two balls, that is, Z := |X1 − X2 |.
Can you find an unbiased estimator of n based on Z? Justify your answer.
[4 marks]
7
CST0.2021.1.8
SECTION D
7 Algorithms
(a) The BST-sort algorithm looks at each element of the array in turn, starting at
position 0, and inserts it into a BST (pass 1). Having processed all elements, it
repeatedly extracts the minimum from the BST, refilling the array from position
0 onwards (pass 2).
(b) The enhanced 2-3-4-sort algorithm is obtained by replacing the BST with a
2-3-4 tree in enhanced BST-sort.
(ii ) How much space will enhanced 2-3-4-sort require to sort an array of n items,
if each item is m bits long? Give exact upper and lower bounds in terms
of n and m rather than an asymptotic estimate. [3 marks]
8
CST0.2021.1.9
8 Algorithms
the k best selling items ever, ranked by how many units were sold;
the k best selling items of the past 30 days, ranked by how many units were
sold;
the k highest-revenue items of this calendar year, ranked by total revenue since
1st of January of this year.
(a) Each item will be held in a record. Describe all the data structures that must
refer to these records to implement the required functionality. Describe all the
fields that the record must have to implement the required functionality, and
how each of these fields has to be updated and when. [5 marks]
(b) The obvious baseline solution is to re-sort the n items and to take the top k
every time the bestseller lists must be produced. Assuming the number of items
and the given average rates stay constant, what is its asymptotic worst-case time
cost per unit time? [1 mark]
(c) Describe three alternative strategies, each better than the baseline, to implement
the required functionality. Use the heaps or search trees of your choice,
explaining precisely what you would store in each data structure to implement
the required functionality. Describe, in each case, how to initialize the data
structures, how to update the data structures after each sale, how to recompute
the three bestseller lists every s seconds, together with the worst-case asymptotic
time cost of each operation as a function of m, n, k, s (cost per unit time for the
second and third operations). [6 marks]
(e) Repeat for n = 1014 and the other parameters unchanged. [3 marks]
(f ) Repeat for m = 10−4 , n = 102 , k = 101 , s = 105 , reasoning about the structural
difference between this and the websites of cases (d ) and (e). [3 marks]
9
CST0.2021.1.10
9 Algorithms
(a) In the graph shown here, find all Pareto efficient paths from s to t, and state
their costs. [1 mark]
(c) Let v0 → · · · vk be a Pareto efficient path from v0 to vk , and let its costs be
(ca , cb ). Show that there is a Pareto efficient path from v0 to vk with costs
(ca , cb ) that has ≤ V − 1 edges, where V is the number of vertices in the graph.
[3 marks]
(d ) We are given a start vertex s. Give an algorithm to compute all costs achievable
by Pareto efficient paths from s to every other vertex. [6 marks]
10
CST0.2021.1.11
10 Algorithms
This question is concerned with connected undirected graphs in which each edge has
a weight, and with spanning trees in such graphs.
(a) Explain what is meant by the translation strategy, and outline briefly the steps
of a translation-based proof of correctness. [3 marks]
(b) Give an algorithm for finding a maximum spanning tree, that runs in O(E +
V log V ) time. Explain why your algorithm’s running time is as required.
[8 marks]
[Note: You may refer to algorithms from lecture notes without quoting the code. You
may use results from lecture notes without proof, but you must state them clearly.]
END OF PAPER
11