Algorithms & complexity worksheet with answers

Free printable Algorithms & complexity worksheet: 8 problems from easy to exam level, with an answer key. Print it or save it as a PDF.

For teachers: free to print, copy and use in class. Every new worksheet has new values and its own answer key. A link to ExamProblems on your course page is appreciated.

Name: ____________________   Date: ____________

1.Easy

Which sorting algorithm chooses a pivot and partitions the array into smaller and larger elements?

  1. AMerge sort
  2. BInsertion sort
  3. CBubble sort
  4. DQuicksort
2.Easy

What is the Big O of 3n² + 5n + 2?

  1. AO(n²)
  2. BO(n³)
  3. CO(3n²)
  4. DO(5n)
3.Easy

What is the worst-case time complexity of merge sort?

  1. AO(n)
  2. BO(log n)
  3. CO(n log n)
  4. DO(n²)
4.Medium

How much stack space does a simple recursive factorial function fact(n) = n × fact(n − 1) use?

  1. AO(log n)
  2. BO(n²)
  3. CO(1)
  4. DO(n)
5.Medium

How does Prim's algorithm build a minimum spanning tree?

  1. AIt runs breadth-first search and keeps the BFS tree
  2. BIt removes the most expensive edge until no cycles remain
  3. CIt grows one tree from a start vertex, adding the cheapest edge that leaves the tree
  4. DIt sorts all edges by weight and adds each one unless it would form a cycle in the forest
6.Medium

A recurrence T(n) = 2T(n/2) + O(n) solves to:

  1. AO(n log n)
  2. BO(log n)
  3. CO(n)
  4. DO(n²)
7.Hard

Taking both endpoints of every edge in a maximal matching gives a vertex cover. How good is this cover?

  1. AOf unknown quality compared to the optimum
  2. BAlways a minimum vertex cover
  3. CAt most log n times the minimum
  4. DAt most twice the size of a minimum vertex cover
8.Hard

The 0/1 knapsack problem solved with dynamic programming takes O(nW) time. Why is this called pseudo-polynomial?

  1. ABecause n is always small
  2. BW is a number, so the time is exponential in the number of bits used to write W
  3. CBecause the algorithm is randomised and its running time varies with each input
  4. DBecause it only works for W = 0

Answer key

  1. 1. D · Quicksort
  2. 2. A · O(n²)
  3. 3. C · O(n log n)
  4. 4. D · O(n)
  5. 5. C · It grows one tree from a start vertex, adding the cheapest edge that leaves the tree
  6. 6. A · O(n log n)
  7. 7. D · At most twice the size of a minimum vertex cover
  8. 8. B · W is a number, so the time is exponential in the number of bits used to write W

Full step-by-step solutions: practise this topic on examproblems.com.