Sample CPSC 221 questions
  1. Worst-Case and Average-Case Analysis · Multiple choice · 1 mark

    The algorithm below returns the largest element of A[1…n]A[1\ldots n], n≥1n\ge1.

    FindMax(A, n)
        m = A[1]
        for i = 2 to n
            if A[i] > m
                m = A[i]
        return m

    Which statement is a loop invariant that is true at the start of every iteration of the for loop (including the first, with i=2i=2) and, together with the loop’s exit condition, proves the algorithm correct?

    1. m=max⁡{A[1],…,A[i−1]}m=\max\{A[1],\ldots,A[i-1]\}

    2. m=max⁡{A[1],…,A[i]}m=\max\{A[1],\ldots,A[i]\}

    3. m≥A[i]m\ge A[i]

    4. m=A[i−1]m=A[i-1]

    Show solution

    Answer: (A)

    Before the iteration with index ii, mm holds the maximum of the elements already examined, A[1],…,A[i−1]A[1],\ldots,A[i-1]. It holds at i=2i=2 because m=A[1]m=A[1], each iteration keeps it true, and when the loop ends with i=n+1i=n+1 it says m=max⁡A[1…n]m=\max A[1\ldots n]. The version that includes A[i]A[i] is false on entry to the iteration (m=A[1]m=A[1] need not equal max⁡{A[1],A[2]}\max\{A[1],A[2]\}); m≥A[i]m\ge A[i] is not maintained (a later element may be larger); m=A[i−1]m=A[i-1] fails whenever A[i−1]A[i-1] is not a new maximum.

  2. Balanced and Augmented Search Trees · Short answer · 2 marks

    In an AVL tree, node zz has bf(z)=+2\mathrm{bf}(z)=+2 and its left child yy has bf(y)=−1\mathrm{bf}(y)=-1. Let yy have left subtree AA and right subtree BB, and let CC be the right subtree of zz. Suppose h(A)=kh(A)=k (with k≥0k\ge 0). A single right rotation about zz is performed (so yy becomes the root, zz its right child with subtrees BB and CC). Give the balance factors of zz and of yy afterwards, as numbers, and say whether the result is an AVL tree.

    Show solution

    bf(y)=−1\mathrm{bf}(y)=-1 means h(B)=h(A)+1=k+1h(B)=h(A)+1=k+1, so h(y)=k+2h(y)=k+2; bf(z)=+2\mathrm{bf}(z)=+2 means h(C)=h(y)−2=kh(C)=h(y)-2=k. After the rotation zz has subtrees BB (k+1k+1) and CC (kk): bf(z)=+1\mathrm{bf}(z)=+1, h(z)=k+2h(z)=k+2. Now yy has subtrees AA (kk) and zz (k+2k+2): bf(y)=−2\mathrm{bf}(y)=-2. The result is not an AVL tree; a double rotation is required.

  3. Priority Queues and Heaps · Multiple choice · 3 marks

    Extract-Max on a max-heap with 3131 keys removes A[1]A[1], moves A[31]A[31] to the root, and sifts it down in a heap of 3030 keys. At each level the sift-down compares the two children with each other (one comparison, none if there is only one child) and then compares the larger child with the moving key (one comparison); it stops when the moving key is at least as large as both children or has no child. What is the largest possible number of key comparisons this Extract-Max can use?

    1. 44

    2. 55

    3. 66

    4. 88

    Show solution

    Answer: (D)

    The heap left after the removal has 3030 keys, so its height is ⌊log⁡230⌋=4\lfloor\log_2 30\rfloor=4 and the moving key can descend at most four levels. Each level costs at most two comparisons, giving 2⋅4=82\cdot 4=8, and this is reached when the moving key is smaller than everything on its path (for instance the minimum moved from the end of a decreasing array). One comparison per level gives 44 but forgets the comparison between siblings; 6=2⋅36=2\cdot 3 uses the height of a 1515-key heap, and 55 is ⌈log⁡231⌉\lceil\log_2 31\rceil.

  4. Disjoint Sets · Multiple choice · 1 mark

    Sets are linked lists with head pointers. Union(x,y)\textsc{Union}(x,y) appends the list of yy to the list of xx with no weighted-union heuristic, and its cost is the number of head pointers it updates. Starting from the ten singletons {1},…,{10}\{1\},\ldots,\{10\}, the sequence Union(2,1), Union(3,2), Union(4,3), …, Union(10,9)\textsc{Union}(2,1),\ \textsc{Union}(3,2),\ \textsc{Union}(4,3),\ \ldots,\ \textsc{Union}(10,9) is performed. What is the total cost of the nine unions?

    1. 99

    2. 3636

    3. 4545

    4. 5555

    Show solution

    Answer: (C)

    In Union(i+1,i)\textsc{Union}(i+1,i) the list being appended is the one containing ii, which already holds ii elements, so the cost is ii. The total is 1+2+⋯+9=9⋅102=451+2+\cdots+9=\dfrac{9\cdot10}{2}=45. The value 99 would be right if a singleton were appended each time, 36=1+⋯+836=1+\cdots+8 stops one union early, and 55=1+⋯+1055=1+\cdots+10 counts a tenth union that does not exist.

  5. Minimum Spanning Trees and Lower Bounds · Short answer · 2 marks

    The complete graph K8K_8 has vertices 1,2,…,81,2,\dots,8, and the edge {i,j}\{i,j\} has weight i+ji+j. What is the weight of a minimum spanning tree of this graph? Give the number and a one- or two-sentence reason.

    Show solution

    The weight is 4242. Root any spanning tree at vertex 11: each vertex j≥2j\ge2 has a parent edge {j,p}\{j,p\} of weight j+p≥j+1j+p\ge j+1, so the tree weighs at least ∑j=28(j+1)=3+4+⋯+9=42\sum_{j=2}^{8}(j+1)=3+4+\dots+9=42. The star centred at vertex 11 has exactly these edges, so it attains the bound.

About CPSC 221 practice exams

CPSC 221 practice exams

Basic Algorithms and Data Structures · University of British Columbia (UBC, UBC Vancouver) · also written CPSC221

Make a free, printable CPSC 221 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 350 original questions and skips the ones you have already seen.

Looking for CPSC 221 past exams?

Start with the past exams and tests your instructor or the UBC library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CPSC 221 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
  • Balanced and Augmented Search Trees · 50 questions
  • Hashing · 50 questions
  • Priority Queues and Heaps · 50 questions
  • Disjoint Sets · 50 questions
  • Graphs, BFS and DFS · 50 questions
  • Minimum Spanning Trees and Lower Bounds · 50 questions

More UBC courses

questionbank.dev is independent and not affiliated with University of British Columbia. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.