Worst-Case and Average-Case Analysis · Multiple choice · 1 mark
The algorithm below returns the largest element of , .
FindMax(A, n)
m = A[1]
for i = 2 to n
if A[i] > m
m = A[i]
return mWhich statement is a loop invariant that is true at the start of every iteration of the for loop (including the first, with ) and, together with the loop’s exit condition, proves the algorithm correct?
Answer: (A)
Before the iteration with index , holds the maximum of the elements already examined, . It holds at because , each iteration keeps it true, and when the loop ends with it says . The version that includes is false on entry to the iteration ( need not equal ); is not maintained (a later element may be larger); fails whenever is not a new maximum.
Balanced and Augmented Search Trees · Short answer · 2 marks
In an AVL tree, node has and its left child has . Let have left subtree and right subtree , and let be the right subtree of . Suppose (with ). A single right rotation about is performed (so becomes the root, its right child with subtrees and ). Give the balance factors of and of afterwards, as numbers, and say whether the result is an AVL tree.
means , so ; means . After the rotation has subtrees () and (): , . Now has subtrees () and (): . The result is not an AVL tree; a double rotation is required.
Priority Queues and Heaps · Multiple choice · 3 marks
Extract-Max on a max-heap with keys removes , moves to the root, and sifts it down in a heap of 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?
Answer: (D)
The heap left after the removal has keys, so its height is and the moving key can descend at most four levels. Each level costs at most two comparisons, giving , 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 but forgets the comparison between siblings; uses the height of a -key heap, and is .
Disjoint Sets · Multiple choice · 1 mark
Sets are linked lists with head pointers. appends the list of to the list of with no weighted-union heuristic, and its cost is the number of head pointers it updates. Starting from the ten singletons , the sequence is performed. What is the total cost of the nine unions?
Answer: (C)
In the list being appended is the one containing , which already holds elements, so the cost is . The total is . The value would be right if a singleton were appended each time, stops one union early, and counts a tenth union that does not exist.
Minimum Spanning Trees and Lower Bounds · Short answer · 2 marks
The complete graph has vertices , and the edge has weight . What is the weight of a minimum spanning tree of this graph? Give the number and a one- or two-sentence reason.
The weight is . Root any spanning tree at vertex : each vertex has a parent edge of weight , so the tree weighs at least . The star centred at vertex has exactly these edges, so it attains the bound.
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.
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.
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.