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

What is recursion?

  1. AA type of data structure
  2. BA sorting method
  3. CA loop that never ends
  4. DA function that calls itself on smaller inputs
2.Easy

Space complexity measures:

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

What is memoisation?

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

You sort an array of n elements once and then answer k membership queries with binary search. What is the total cost?

  1. AO(nk)
  2. BO(n log n + k log n)
  3. CO(k n log n)
  4. DO(n + k)
5.Medium

Why does dynamic programming give no real speed-up for merge sort?

  1. AIts subproblems do not overlap, so there is nothing to reuse
  2. BDynamic programming only works on graph problems, not on arrays
  3. CMerge sort has no recursive structure
  4. DMerge sort is already O(n)
6.Medium

The time complexity of naive recursive Fibonacci (fib(n) = fib(n−1) + fib(n−2)) is:

  1. AConstant
  2. BLinear
  3. CLogarithmic
  4. DExponential
7.Hard

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

  1. ADijkstra's algorithm
  2. BFloyd–Warshall
  3. CPrim's algorithm starting from one node
  4. DKruskal's algorithm
8.Hard

Is 22n in O(2ⁿ)?

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

Answer key

  1. 1. D · A function that calls itself on smaller inputs
  2. 2. D · The amount of memory an algorithm needs as input grows
  3. 3. A · Storing the results of function calls so repeated calls are answered from memory
  4. 4. B · O(n log n + k log n)
  5. 5. A · Its subproblems do not overlap, so there is nothing to reuse
  6. 6. D · Exponential
  7. 7. D · Kruskal's algorithm
  8. 8. B · No, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound

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