Sample CS 225 questions
  1. Worst-Case and Average-Case Analysis · Short answer · 3 marks

    The array A[1…n]A[1\ldots n] holds nn distinct keys in uniformly random order. A descent is an index i<ni<n with A[i]>A[i+1]A[i]>A[i+1]. What is the expected number of descents? Justify with indicator variables, and evaluate it for n=10n=10.

    Show solution

    Let Xi=1X_i=1 if A[i]>A[i+1]A[i]>A[i+1]. For two distinct keys in random order, either one is larger than the other with probability 12\tfrac12, so E[Xi]=12E[X_i]=\tfrac12. By linearity of expectation, E[descents]=∑i=1n−112=n−12E[\text{descents}]=\sum_{i=1}^{n-1}\tfrac12=\frac{n-1}{2}; for n=10n=10 this is 4.54.5. (The XiX_i are not independent, but linearity does not need independence.)

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

    Let NhN_h be the fewest nodes in an AVL tree of height hh (an empty tree has height −1-1, a single node height 00). What is the greatest possible height of an AVL tree with exactly 20 nodes?

    1. 44

    2. 55

    3. 66

    4. 77

    Show solution

    Answer: (B)

    Nh=Nh−1+Nh−2+1N_h=N_{h-1}+N_{h-2}+1 with N0=1N_0=1, N1=2N_1=2 gives 1,2,4,7,12,20,33,…1,2,4,7,12,20,33,\dots. A tree of height 5 needs at least N5=20N_5=20 nodes, so it can be built with exactly 20; height 6 needs at least 33. The answer 4 is ⌊log⁡220⌋\lfloor\log_2 20\rfloor, the least possible height; 6 comes from applying the bound 1.44log⁡2(n+2)≈6.41.44\log_2(n+2)\approx 6.4 as if it were exact, and 7 from rounding that bound up.

  3. Hashing · Multiple choice · 1 mark

    Four keys are hashed independently and uniformly into a table with m=2m=2 slots. Call a key shared if at least one other key hashes to the same slot. What is the expected number of shared keys?

    1. 22

    2. 33

    3. 3.53.5

    4. 44

    Show solution

    Answer: (C)

    (C). A given key is not shared only if the other three keys all go to the other slot: probability (1/2)3=1/8(1/2)^3=1/8. So it is shared with probability 7/87/8, and by linearity of expectation the expected number of shared keys is 4⋅7/8=3.54\cdot 7/8=3.5. The value 3=(42)/23=\binom42/2 is the expected number of colliding pairs, 2=n/m2=n/m is the load factor, and 44 would need every key shared in every outcome, which fails when the keys split 11–33 between the slots.

  4. Amortized Analysis · Multiple choice · 1 mark

    A queue is built from two stacks, In\mathit{In} and Out\mathit{Out}. Enqueue(xx) pushes xx onto In\mathit{In}. Dequeue first checks Out\mathit{Out}: if it is empty, it pops every element of In\mathit{In} and pushes each onto Out\mathit{Out}; then it pops and returns the top of Out\mathit{Out}. Counting each push and each pop as one primitive operation, how many primitive operations are performed on a given element from its Enqueue until the Dequeue that returns it?

    1. 22

    2. 33

    3. 44

    4. 55

    Show solution

    Answer: (C)

    Every element is pushed onto In\mathit{In}, later popped from In\mathit{In} and pushed onto Out\mathit{Out} during a transfer (it cannot be returned directly from In\mathit{In}), and finally popped from Out\mathit{Out}: four primitives, always. This constant per-element cost is why nn operations cost O(n)O(n) in total even though one Dequeue can cost Θ(n)\Theta(n). Only counting the push and the final pop gives 22.

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

    Every edge of the complete graph K4K_4 has weight 11. How many different minimum spanning trees does it have? Justify by counting the possible shapes.

    Show solution

    Every spanning tree has 33 edges and weight 33, so all spanning trees are minimum. A tree on 44 labelled vertices is either a star (44 choices of the centre) or a path (4!/2=124!/2=12 distinct paths, since a path and its reverse coincide). In all there are 4+12=164+12=16 minimum spanning trees.

About CS 225 practice exams

CS 225 practice exams

Data Structures · University of Illinois Urbana-Champaign (UIUC, Illinois, U of I) · also written CS225

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

Looking for CS 225 past exams?

Start with the past exams and tests your instructor or the UIUC 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 225 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
  • 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
  • Amortized Analysis · 50 questions
  • Disjoint Sets · 50 questions
  • Graphs, BFS and DFS · 50 questions
  • Minimum Spanning Trees and Lower Bounds · 50 questions

More UIUC courses

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