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 design strategy does merge sort use?

  1. ADivide and conquer
  2. BBrute force
  3. CDynamic programming
  4. DGreedy choice
2.Easy

How does selection sort work?

  1. AIt repeatedly finds the minimum of the unsorted part and swaps it to the front
  2. BIt inserts each element into a growing sorted part by shifting
  3. CIt repeatedly swaps neighbouring elements that are in the wrong order
  4. DIt splits the array in halves and merges them
3.Easy

How much extra memory does a standard array-based merge sort use?

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

How many levels does the recursion of merge sort have for an array of n elements?

  1. AAbout √n
  2. BAbout n / 2
  3. CAbout n
  4. DAbout log₂ n
5.Medium

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

  1. AIt sorts all edges by weight and adds each one unless it would form a cycle in the forest
  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 runs breadth-first search and keeps the BFS tree
6.Medium

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

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

Why is the average lookup in a hash table O(1) but the worst case O(n)?

  1. AArrays have O(n) access
  2. BKeys must be sorted first
  3. CHash functions are slow
  4. DMany keys may collide into the same bucket
8.Hard

Which technique gives an expected O(n log n) running time for quicksort on any input?

  1. AChoosing the pivot uniformly at random
  2. BUsing recursion only on the left side
  3. CSorting the input before running quicksort
  4. DAlways choosing the first element

Answer key

  1. 1. A · Divide and conquer
  2. 2. A · It repeatedly finds the minimum of the unsorted part and swaps it to the front
  3. 3. C · O(n)
  4. 4. D · About log₂ n
  5. 5. C · It grows one tree from a start vertex, adding the cheapest edge that leaves the tree
  6. 6. D · O(n log n)
  7. 7. D · Many keys may collide into the same bucket
  8. 8. A · Choosing the pivot uniformly at random

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