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 of these running times is polynomial?

  1. AO(n³)
  2. BO(nⁿ)
  3. CO(n!)
  4. DO(2ⁿ)
2.Easy

What is memoisation?

  1. AFreeing memory after a function returns
  2. BStoring the results of function calls so repeated calls are answered from memory
  3. CWriting comments next to each line of code
  4. DConverting recursion into an equivalent loop automatically so no call stack is needed
3.Easy

Space complexity measures:

  1. AThe number of lines of code
  2. BThe disk space of the program file once it is compiled
  3. CThe physical size of the computer
  4. DThe amount of memory an algorithm needs as input grows
4.Medium

Which sorting algorithm is stable?

  1. AMerge sort
  2. BHeap sort
  3. CQuicksort (typical in-place version)
  4. DSelection sort (typical version)
5.Medium

Which statement about a recursive function without a reachable base case is correct?

  1. AIt keeps calling itself until the call stack overflows
  2. BIt returns 0 automatically
  3. CIt runs in O(1) time
  4. DThe compiler always turns it into a loop that ends safely
6.Medium

What is the time complexity of the standard dynamic programming algorithm for the longest common subsequence of strings of lengths m and n?

  1. AO(2m+n)
  2. BO(m + n)
  3. CO(mn log n)
  4. DO(mn)
7.Hard

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

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

Is 22n in O(2ⁿ)?

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

Answer key

  1. 1. A · O(n³)
  2. 2. B · Storing the results of function calls so repeated calls are answered from memory
  3. 3. D · The amount of memory an algorithm needs as input grows
  4. 4. A · Merge sort
  5. 5. A · It keeps calling itself until the call stack overflows
  6. 6. D · O(mn)
  7. 7. A · W is a number, so the time is exponential in the number of bits used to write W
  8. 8. C · No, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound

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