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

How does selection sort work?

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

What does it mean if an operation runs in O(1) time?

  1. AIt processes exactly one element per second of running time
  2. BIts running time does not grow with the input size
  3. CIt always takes exactly one second
  4. DIt can only be run once
3.Easy

What is memoisation?

  1. AConverting recursion into an equivalent loop automatically so no call stack is needed
  2. BFreeing memory after a function returns
  3. CStoring the results of function calls so repeated calls are answered from memory
  4. DWriting comments next to each line of code
4.Medium

What is the time complexity of: for i = 1 to n: (j = 1; while j < n: j = j × 2)?

  1. AO(n log n)
  2. BO(n)
  3. CO(log n)
  4. DO(n²)
5.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²)
6.Medium

With coins of 1, 3 and 4 and an amount of 6, how many coins does the greedy "largest coin first" method use, compared to the optimum?

  1. AGreedy uses 2 coins; the optimum is 2
  2. BGreedy uses 4 coins; the optimum is 3
  3. CGreedy uses 3 coins; the optimum is 2
  4. DGreedy uses 3 coins; the optimum is 3
7.Hard

Which algorithm finds a minimum spanning tree by repeatedly adding the cheapest edge that does not form a cycle?

  1. APrim's algorithm starting from one node
  2. BDijkstra's algorithm
  3. CFloyd–Warshall
  4. DKruskal's algorithm
8.Hard

Is 22n in O(2ⁿ)?

  1. ANo, because 22n grows more slowly than 2ⁿ
  2. BYes, because both are exponential
  3. CYes, because constant factors in the exponent are always ignored in Big O
  4. DNo, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound

Answer key

  1. 1. D · It repeatedly finds the minimum of the unsorted part and swaps it to the front
  2. 2. B · Its running time does not grow with the input size
  3. 3. C · Storing the results of function calls so repeated calls are answered from memory
  4. 4. A · O(n log n)
  5. 5. B · O(n)
  6. 6. C · Greedy uses 3 coins; the optimum is 2
  7. 7. D · Kruskal's algorithm
  8. 8. D · No, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound

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