Worst-Case and Average-Case Analysis · Multiple choice · 2 marks
How many times is the statement count = count + 1 executed, as a function of ?
count = 0
for i = 1 to n
j = i
while j <= n
count = count + 1
j = j + iAnswer: (A)
For a fixed the values of are up to , so the inner loop runs times. The total is
Randomized Algorithms and Quicksort · Multiple choice · 2 marks
The following version of randomized quicksort is run as Quicksort(A, 1, 10) on distinct keys.
Quicksort(A, p, r):
if p > r: return
choose q uniformly at random from p..r
swap A[q] and A[r]
q = Partition(A, p, r)
Quicksort(A, p, q - 1)
Quicksort(A, q + 1, r)Counting the first call and all calls on empty ranges, how many calls to Quicksort are made in total?
Answer: (C)
(C). Every call on a non-empty range puts its pivot into its final position, so there are exactly such calls, whatever the pivots are. Each of them makes two further calls, so the total is (equivalently, a binary tree with internal nodes has empty subtrees). The value counts only non-empty calls, forgets the first call, and is the number of pairs of keys, not of calls.
Skip Lists, Self-Organizing Lists and B-Trees · Short answer · 2 marks
A skip list stores keys. Each key is on level , and a key on level appears on level with probability , independently. Ignore the two sentinel towers.
(a) Expected number of keys on level or higher:
(b) Expected number of nodes on level :
(c) Expected total number of nodes over all levels:
Answer: ; ;
A key is on level with probability , so level has nodes in expectation: on level (and higher), on level . Summing over all levels, , that is nodes, so the expected space is .
Range Search and Multidimensional Data · Multiple choice · 1 mark
The primary tree of a 2D range tree is a perfect binary tree with leaves, one point per leaf, sorted by -coordinate. Every node, the leaves included, stores an associated structure holding the points in its subtree. In how many associated structures does a given point appear?
Answer: (B)
A point is in the associated structure of its leaf and of each ancestor of the leaf. A perfect tree with leaves has height , so a leaf has proper ancestors, giving nodes on the path. Answering forgets the leaf’s own structure, doubles the path (counting a left and a right copy), and would be the size of the root’s structure.
Data Compression · Short answer · 2 marks
Huffman’s algorithm on the counts , , , , performs four merges that create trees of weights , , and .
Without finding any codeword, how many bits does the Huffman encoding of the text need? [1]
Explain in one or two sentences why your method works. [1]
bits.
The weight of an internal node is the total frequency of the leaves below it. A symbol of frequency whose leaf has depth lies below exactly internal nodes (its ancestors), so it contributes to the weight of each of them, in all. Summing over the internal nodes therefore gives , the length of the encoded text.
Data Structures and Data Management · University of Waterloo (Waterloo, UWaterloo, UW) · also written CS240
Make a free, printable CS 240 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 450 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the Waterloo library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CS 240 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with University of Waterloo. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.