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 does it mean if an operation runs in O(1) time?

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

A problem is usually called tractable if:

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

What is the time complexity of multiplying two n × n matrices with the standard triple loop?

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

Which sorting algorithm is stable?

  1. ASelection sort (typical version)
  2. BHeap sort
  3. CQuicksort (typical in-place version)
  4. DMerge sort
6.Medium

A function runs one loop of n iterations and then, separately, another loop of n iterations. What is its time complexity?

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

Which statement about P and NP is correct?

  1. ANP means "not polynomial"
  2. BNP problems cannot be verified quickly
  3. CIt has been proven that P ≠ NP
  4. DEvery problem in P is also in NP
8.Hard

Strassen's algorithm multiplies matrices using 7 instead of 8 recursive multiplications of half-size blocks. What is its running time?

  1. AΘ(n² log n)
  2. BΘ(nlog₂ 7), about n2.81
  3. CΘ(n2)
  4. DΘ(n³)

Answer key

  1. 1. B · Its running time does not grow with the input size
  2. 2. D · Storing the results of function calls so repeated calls are answered from memory
  3. 3. A · It can be solved in polynomial time
  4. 4. B · O(n³)
  5. 5. D · Merge sort
  6. 6. A · O(n)
  7. 7. D · Every problem in P is also in NP
  8. 8. B · Θ(nlog₂ 7), about n2.81

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