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 memoisation?

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

A problem is usually called tractable if:

  1. AIt has no solution
  2. BIt can be solved in polynomial time
  3. CIt can be solved in constant time
  4. DIt can only be solved by trying every possibility
3.Easy

What is the base case of a recursive function?

  1. AThe case with the largest input, which is handled before any recursive calls
  2. BThe call that uses the most memory
  3. CThe first line of the function
  4. DA case that is answered directly without making another recursive call
4.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. BMerge sort has no recursive structure
  3. CDynamic programming only works on graph problems, not on arrays
  4. DMerge sort is already O(n)
5.Medium

What is the amortised time complexity of appending to a dynamic array that doubles its capacity when full?

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

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

  1. ALogarithmic
  2. BExponential
  3. CLinear
  4. DConstant
7.Hard

For which knapsack variant does the greedy rule "take items in order of value per weight" always give an optimal answer?

  1. AThe 0/1 knapsack, where items must always be taken whole
  2. BBoth variants
  3. CThe fractional knapsack, where items may be split
  4. DNeither variant
8.Hard

Which statement about P and NP is correct?

  1. AIt has been proven that P ≠ NP
  2. BNP means "not polynomial"
  3. CEvery problem in P is also in NP
  4. DNP problems cannot be verified quickly

Answer key

  1. 1. D · Storing the results of function calls so repeated calls are answered from memory
  2. 2. B · It can be solved in polynomial time
  3. 3. D · A case that is answered directly without making another recursive call
  4. 4. A · Its subproblems do not overlap, so there is nothing to reuse
  5. 5. A · O(1)
  6. 6. B · Exponential
  7. 7. C · The fractional knapsack, where items may be split
  8. 8. C · Every problem in P is also in NP

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