University

Algorithms Practice Exam — Free

Generate a realistic AI practice exam with worked solutions

Generate Your Free Practice Exam

Algorithms covers asymptotic analysis, sorting, greedy methods, divide and conquer, dynamic programming, and graph algorithms. Exams ask you to design, trace, and prove — often all three in one question. Practicing full problems with worked solutions teaches the standard argument patterns (exchange arguments, DP tables, invariants) that graders look for.

Crameleon generates your practice exam from your own course materials — lecture notes, syllabi, past exams, or any combination. Upload what you have and get a fully formatted exam with the same structure as your professor's actual tests: multiple choice, short answer, and worked problems, all with step-by-step solutions (first 2 free; all with Premium).

Unlike generic question banks, every Crameleon exam reflects the specific framing, terminology, and difficulty level of your course. You'll practise answering questions that look and feel like the real thing — which is exactly what the research on retrieval practice says you should be doing. Students who test themselves with practice exams consistently outperform students who re-read notes, regardless of study time. The generation takes under 2 minutes and your first exam is completely free.

Topics typically tested

  • Asymptotic notation & recurrences
  • Divide & conquer (mergesort, quicksort)
  • Greedy algorithms & exchange arguments
  • Dynamic programming
  • Graph traversal (BFS/DFS) & shortest paths
  • Minimum spanning trees

Sample Algorithms exam questions

Three original questions in the style of a real Algorithms exam, each with a worked solution. Your own practice exam can be built from your course's notes and syllabus.

Question 1Multiple choiceAsymptotic notation & recurrences

The running time of a divide-and-conquer algorithm satisfies

T(n)=2 T ⁣(n4)+n,T(1)=1.T(n) = 2\,T\!\left(\frac{n}{4}\right) + \sqrt{n}, \qquad T(1) = 1.

What is the tight asymptotic bound on T(n)T(n)?

  1. A.

    Θ(n)\Theta(\sqrt{n})

  2. B.

    Θ(n log⁡n)\Theta(\sqrt{n}\,\log n)

  3. C.

    Θ(n)\Theta(n)

  4. D.

    Θ(nlog⁡n)\Theta(n \log n)

Worked solution
Answer:B

This recurrence has the Master Theorem form T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n) with a=2a = 2, b=4b = 4 and f(n)=n1/2f(n) = n^{1/2}. The critical exponent is

log⁡ba=log⁡42=12,\log_b a = \log_4 2 = \tfrac{1}{2},

so nlog⁡ba=n1/2n^{\log_b a} = n^{1/2}, exactly the same growth as f(n)f(n). That is case 2 (the work at every level matches the work at the leaves), which gives T(n)=Θ(nlog⁡balog⁡n)=Θ(nlog⁡n)T(n) = \Theta(n^{\log_b a}\log n) = \Theta(\sqrt{n}\log n).

Unrolling the recursion confirms it. At depth kk there are 2k2^k subproblems, each of size n/4kn/4^k and cost n/4k=n/2k\sqrt{n/4^k} = \sqrt{n}/2^k, so each level costs 2k⋅n/2k=n2^k\cdot\sqrt{n}/2^k = \sqrt{n}. There are log⁡4n\log_4 n levels before the size reaches 1, so the total is nlog⁡4n\sqrt{n}\log_4 n plus 2log⁡4n=n2^{\log_4 n} = \sqrt{n} for the leaves, which is Θ(nlog⁡n)\Theta(\sqrt{n}\log n). Numerically, for n=410n = 4^{10} the exact solution is n (log⁡4n+1)=1024×11=11 264\sqrt{n}\,(\log_4 n + 1) = 1024\times 11 = 11\,264, whereas n\sqrt{n} alone is only 1024.

Option A is the tempting answer: it is what you get by assuming the root or the leaves dominate (cases 3 and 1), but here every level does equal work, and log⁡n\log n equal levels add a factor of log⁡n\log n. Options C and D would require f(n)f(n) to grow polynomially faster than n\sqrt{n}, which it does not.

T(n)=Θ(nlog⁡n)T(n) = \Theta(\sqrt{n}\log n) (option B).

Question 2Short answerMinimum spanning trees

An undirected weighted graph has vertices A, B, C, D, E, F and the edges A–B (3), A–C (1), B–C (7), B–D (5), C–D (12), C–E (8), D–E (9), D–F (4), E–F (2). Run Kruskal's algorithm on it. List the edges in the order they are added to the minimum spanning tree, give the total weight of the tree, and name the first edge that Kruskal's algorithm examines and rejects.

Worked solution
Answer:

A–C, E–F, A–B, D–F, B–D; total weight 15; B–C (7) is the first edge rejected

Kruskal's algorithm sorts the edges by weight and adds each edge whose endpoints lie in different components (checked with union-find), skipping any edge that would close a cycle.

Sorted edges: A–C (1), E–F (2), A–B (3), D–F (4), B–D (5), B–C (7), C–E (8), D–E (9), C–D (12).

  • A–C (1): components {A, C}. Add.
  • E–F (2): {A, C}, {E, F}. Add.
  • A–B (3): B joins A's component: {A, B, C}, {E, F}. Add.
  • D–F (4): D joins F's component: {A, B, C}, {D, E, F}. Add.
  • B–D (5): merges the two components into {A, B, C, D, E, F}. Add.

Five edges now connect six vertices, which is ∣V∣−1|V| - 1, so the tree is complete and the algorithm can stop. The next edge, B–C (7), is the first one examined and rejected: B and C are already connected (through A), so adding it would form a cycle. C–E, D–E and C–D are rejected for the same reason.

Total weight: 1+2+3+4+5=151+2+3+4+5 = 15.

Because all edge weights are distinct, the minimum spanning tree is unique, and Prim's algorithm started from A confirms it: it picks A–C (1), then A–B (3), then B–D (5), then D–F (4), then F–E (2), the same five edges.

Edges added: A–C, E–F, A–B, D–F, B–D; total weight 15; B–C (7) is the first edge rejected.

Question 3Worked problemDynamic programming

A currency has coins of value 1, 4 and 6. Let c(x)c(x) be the minimum number of coins whose values sum to exactly xx. (a) Write a recurrence for c(x)c(x) together with its base case. (b) Fill in c(0),c(1),…,c(14)c(0), c(1), \dots, c(14) bottom-up. (c) Give c(14)c(14) and a set of coins that achieves it. (d) Show that the greedy strategy (always take the largest coin that fits) is not optimal for x=14x = 14.

Worked solution
Answer:

c(x)=1+min⁡{c(x−1),c(x−4),c(x−6)}c(x) = 1 + \min\{c(x-1), c(x-4), c(x-6)\} over the coins that fit, c(0)=0c(0) = 0; table 0, 1, 2, 3, 1, 2, 1, 2, 2, 3, 2, 3, 2, 3, 3; c(14)=3c(14) = 3 using 6 + 4 + 4; greedy gives 6 + 6 + 1 + 1, four coins

(a) In an optimal way of making xx, the last coin has value d∈{1,4,6}d \in \{1, 4, 6\}, and removing it must leave an optimal way of making x−dx - d (otherwise swapping in a better one would improve the total). This optimal substructure gives

c(x)=1+min⁡d∈{1,4,6},  d≤xc(x−d),c(0)=0.c(x) = 1 + \min_{d \in \{1,4,6\},\; d \le x} c(x - d), \qquad c(0) = 0.

(b) Fill the table in increasing xx, using only entries already computed:

xx01234567891011121314
c(x)c(x)012312122323233

For example c(4)=1+min⁡(c(3),c(0))=1c(4) = 1 + \min(c(3), c(0)) = 1, c(8)=1+min⁡(c(7),c(4),c(2))=1+1=2c(8) = 1 + \min(c(7), c(4), c(2)) = 1 + 1 = 2, c(10)=1+min⁡(c(9),c(6),c(4))=2c(10) = 1 + \min(c(9), c(6), c(4)) = 2, and c(14)=1+min⁡(c(13),c(10),c(8))=1+min⁡(3,2,2)=3c(14) = 1 + \min(c(13), c(10), c(8)) = 1 + \min(3, 2, 2) = 3.

(c) c(14)=3c(14) = 3. Trace back through the choices: c(14)c(14) came from c(8)c(8) with a 6 (or equally from c(10)c(10) with a 4), c(8)c(8) from c(4)c(4) with a 4, and c(4)c(4) from c(0)c(0) with a 4. The coins are 6 + 4 + 4 = 14. No two coins can reach 14, since the largest two sum to 12, so 3 is indeed optimal.

(d) Greedy takes 6, then another 6 (leaving 2), then 1 and 1: four coins, 6 + 6 + 1 + 1. The dynamic-programming solution uses three, so greedy is not optimal for this coin system. It fails because taking the second 6 leaves a remainder (2) that only the 1-coins can cover, whereas giving up one 6 for two 4s covers the remainder exactly.

c(x)=1+min⁡{c(x−1),c(x−4),c(x−6)}c(x) = 1 + \min\{c(x-1), c(x-4), c(x-6)\} with c(0)=0c(0) = 0; c(14)=3c(14) = 3 using coins 6, 4, 4; greedy needs 4 coins.

Used by 3,000+ students across Canadian universities·Step-by-step solutions (first 2 free)·Works for any course

Ready to practice?

Generate Your Free Algorithms Practice Exam

Your first exam is free — just sign in, no credit card.