Sample ECE 250 questions
  1. Worst-Case and Average-Case Analysis · Short answer · 2 marks

    Solve T(n)=T(n−1)+3T(n)=T(n-1)+3 for n≥2n\ge2, with T(1)=2T(1)=2, by unrolling. Give the closed form and its Θ\Theta class.

    Show solution

    Unrolling kk times gives T(n)=T(n−k)+3kT(n)=T(n-k)+3k. Taking k=n−1k=n-1 gives T(n)=T(1)+3(n−1)=3n−1T(n)=T(1)+3(n-1)=3n-1, which is Θ(n)\Theta(n).

  2. Balanced and Augmented Search Trees · Multiple choice · 2 marks

    Which statement about the number of rotations in an AVL tree with nn keys is correct?

    1. Both insertion and deletion need at most one single or double rotation, because the heights of subtrees change by at most 1

    2. Insertion can need a rotation at every level of the search path, while deletion needs at most one

    3. Both insertion and deletion can need a rotation at every level, because a height change can travel to the root

    4. Insertion needs at most one single or double rotation; deletion can need a rotation at each of Θ(log⁡n)\Theta(\log n) levels

    Show solution

    Answer: (D)

    After an insertion, the first unbalanced node on the way up is repaired by a single or double rotation that restores the subtree to its height before the insertion, so no ancestor above it changes height: at most one restructuring. A deletion can leave the repaired subtree one level shorter than before, so the imbalance can reappear at the next ancestor, and so on up to the root (a Fibonacci-shaped tree with one leaf removed does this).

  3. Hashing · Problem · 4 marks

    Let nn keys be hashed independently and uniformly into a table with mm slots (simple uniform hashing). Let XX be the number of pairs of keys that hash to the same slot.

    1. For i<ji<j let Xij=1X_{ij}=1 if keys ii and jj collide and 00 otherwise. Find E[Xij]E[X_{ij}] and use linearity of expectation to find E[X]E[X] in terms of nn and mm. [2]

    2. For n=30n=30 keys, what is the smallest integer mm for which E[X]<12E[X]<\tfrac12? [1]

    3. For that mm, use Markov’s inequality to give a lower bound on the probability that no two of the 3030 keys collide. [1]

    Show solution
    1. Given where key ii goes, key jj lands in the same slot with probability 1/m1/m, so E[Xij]=1/mE[X_{ij}]=1/m. There are (n2)\binom n2 pairs and X=∑i<jXijX=\sum_{i<j}X_{ij}, hence E[X]=(n2)⋅1m=n(n−1)2mE[X]=\binom n2\cdot\frac1m=\dfrac{n(n-1)}{2m}.

    2. (302)=435\binom{30}{2}=435, so we need 435/m<1/2435/m<1/2, i.e. m>870m>870. The smallest integer is m=871m=871.

    3. Markov: Pr⁡[X≥1]≤E[X]=435/871<12\Pr[X\ge1]\le E[X]=435/871<\tfrac12 (it is ≈0.4994\approx0.4994). So Pr⁡[no collision]=1−Pr⁡[X≥1]≥1−435871=436871>12\Pr[\text{no collision}]=1-\Pr[X\ge1]\ge 1-\frac{435}{871}=\frac{436}{871}>\frac12.

  4. Randomized Algorithms and Quicksort · Short answer · 2 marks

    Randomized quicksort sorts 66 distinct keys. Out of the (62)=15\binom62=15 pairs of keys, what is the expected number of pairs that are never compared with each other? Give an exact fraction.

    Show solution

    4710\dfrac{47}{10}. A pair at distance d=j−id=j-i is compared with probability 2d+1\frac{2}{d+1}. The numbers of pairs at distance 1,…,51,\dots,5 are 5,4,3,2,15,4,3,2,1, so the expected number of compared pairs is 5+83+32+45+13=103105+\frac83+\frac32+\frac45+\frac13=\frac{103}{10}, and the expected number of pairs never compared is 15−10310=471015-\frac{103}{10}=\frac{47}{10}.

  5. Minimum Spanning Trees and Lower Bounds · Multiple choice · 1 mark

    A sorted array holds n=100n=100 distinct keys. An algorithm locates a query xx using only yes/no questions of the form “is x<A[i]x<A[i]?” and must report which of the n+1n+1 gaps (before A[1]A[1], between neighbours, after A[100]A[100]) contains xx. What does the decision-tree argument give as a lower bound on the worst-case number of questions?

    1. 66

    2. 77

    3. 1414

    4. 100100

    Show solution

    Answer: (B)

    A binary tree with n+1=101n+1=101 distinct possible answers needs at least 101101 leaves, so its height is at least ⌈log⁡2101⌉=7\lceil\log_2 101\rceil=7 (binary search achieves 77). The value 6=⌊log⁡2100⌋6=\lfloor\log_2 100\rfloor rounds down and 26=64<1012^6=64<101 leaves cannot cover all gaps. 1414 doubles the bound for no reason and 100100 is the cost of a linear scan.

About ECE 250 practice exams

ECE 250 practice exams

Algorithms and Data Structures · University of Waterloo (Waterloo, UWaterloo, UW) · also written ECE250

Make a free, printable ECE 250 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 ECE 250 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 ECE 250 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
  • Priority Queues and Heaps · 50 questions
  • Hashing · 50 questions
  • Randomized Algorithms and Quicksort · 50 questions
  • Graphs, BFS and DFS · 50 questions
  • Minimum Spanning Trees and Lower Bounds · 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.