Sample CS 240 questions
  1. Worst-Case and Average-Case Analysis · Multiple choice · 2 marks

    How many times is the statement count = count + 1 executed, as a function of nn?

    count = 0
    for i = 1 to n
        j = i
        while j <= n
            count = count + 1
            j = j + i
    1. Θ(nlog⁡n)\Theta(n\log n)

    2. Θ(n3/2)\Theta(n^{3/2})

    3. Θ(n2)\Theta(n^2)

    4. Θ(n)\Theta(n)

    Show solution

    Answer: (A)

    For a fixed ii the values of jj are i,2i,3i,…i,2i,3i,\ldots up to nn, so the inner loop runs ⌊n/i⌋\lfloor n/i\rfloor times. The total is

    ∑i=1n⌊n/i⌋ ≤ n∑i=1n1i=nHn∈Θ(nlog⁡n), \sum_{i=1}^n \lfloor n/i\rfloor\ \le\ n\sum_{i=1}^n \frac1i = n H_n\in\Theta(n\log n),
    and it is at least nHn−nn H_n-n. Treating the inner loop as running nn times gives n2n^2; treating it as constant gives nn; both ignore that its length shrinks like n/in/i.

  2. Randomized Algorithms and Quicksort · Multiple choice · 2 marks

    The following version of randomized quicksort is run as Quicksort(A, 1, 10) on 1010 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?

    1. 1010

    2. 1919

    3. 2121

    4. 4545

    Show solution

    Answer: (C)

    (C). Every call on a non-empty range puts its pivot into its final position, so there are exactly 1010 such calls, whatever the pivots are. Each of them makes two further calls, so the total is 1+2⋅10=211+2\cdot 10=21 (equivalently, a binary tree with 1010 internal nodes has 1111 empty subtrees). The value 1010 counts only non-empty calls, 1919 forgets the first call, and 4545 is the number of pairs of keys, not of calls.

  3. Skip Lists, Self-Organizing Lists and B-Trees · Short answer · 2 marks

    A skip list stores n=1000n=1000 keys. Each key is on level 00, and a key on level ii appears on level i+1i+1 with probability 1/21/2, independently. Ignore the two sentinel towers.

    (a) Expected number of keys on level 11 or higher:    

    (b) Expected number of nodes on level 33:    

    (c) Expected total number of nodes over all levels:    

    Show solution

    Answer: 500500; 125125; 20002000

    A key is on level ii with probability 2−i2^{-i}, so level ii has 1000/2i1000/2^i nodes in expectation: 500500 on level 11 (and higher), 125125 on level 33. Summing over all levels, 1000 (1+12+14+⋯ )=1000⋅2=20001000\,(1+\frac12+\frac14+\cdots)=1000\cdot 2=2000, that is 2n2n nodes, so the expected space is Θ(n)\Theta(n).

  4. Range Search and Multidimensional Data · Multiple choice · 1 mark

    The primary tree of a 2D range tree is a perfect binary tree with n=512n=512 leaves, one point per leaf, sorted by xx-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?

    1. 99

    2. 1010

    3. 1818

    4. 512512

    Show solution

    Answer: (B)

    A point is in the associated structure of its leaf and of each ancestor of the leaf. A perfect tree with 512=29512=2^9 leaves has height 99, so a leaf has 99 proper ancestors, giving 9+1=109+1=10 nodes on the path. Answering 99 forgets the leaf’s own structure, 1818 doubles the path (counting a left and a right copy), and 512512 would be the size of the root’s structure.

  5. Data Compression · Short answer · 2 marks

    Huffman’s algorithm on the counts A: 2A{:}\,2, B: 3B{:}\,3, C: 7C{:}\,7, D: 8D{:}\,8, E: 15E{:}\,15 performs four merges that create trees of weights 55, 1212, 2020 and 3535.

    1. Without finding any codeword, how many bits does the Huffman encoding of the text need? [1]

    2. Explain in one or two sentences why your method works. [1]

    Show solution
    1. 5+12+20+35=725+12+20+35=72 bits.

    2. The weight of an internal node is the total frequency of the leaves below it. A symbol of frequency ff whose leaf has depth dd lies below exactly dd internal nodes (its ancestors), so it contributes ff to the weight of each of them, f⋅df\cdot d in all. Summing over the internal nodes therefore gives ∑cf(c) d(c)\sum_c f(c)\,d(c), the length of the encoded text.

About CS 240 practice exams

CS 240 practice exams

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.

Looking for CS 240 past exams?

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.

Topics covered

  • Worst-Case and Average-Case Analysis · 50 questions
  • Priority Queues and Heaps · 50 questions
  • Randomized Algorithms and Quicksort · 50 questions
  • Balanced and Augmented Search Trees · 50 questions
  • Skip Lists, Self-Organizing Lists and B-Trees · 50 questions
  • Hashing · 50 questions
  • Range Search and Multidimensional Data · 50 questions
  • Tries and String Matching · 50 questions
  • Data Compression · 50 questions

More Waterloo courses

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.