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 the best-case time complexity of insertion sort, on an already sorted array?

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

An O(n²) algorithm takes 1 second for n = 1000. About how long does it take for n = 2000?

  1. A4 seconds
  2. B2 seconds
  3. C8 seconds
  4. D1 second
3.Easy

What is the base case of a recursive function?

  1. AThe call that uses the most memory
  2. BThe first line of the function
  3. CA case that is answered directly without making another recursive call
  4. DThe case with the largest input, which is handled before any recursive calls
4.Medium

Which sorting algorithm is stable?

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

What does the recurrence T(n) = T(n − 1) + O(1) solve to?

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

What does the recurrence T(n) = 2T(n/2) + O(1) solve to?

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

Which definition of the class NP is correct?

  1. AProblems that cannot be solved in polynomial time
  2. BDecision problems whose yes-answers can be verified in polynomial time given a certificate
  3. CProblems that are not polynomial
  4. DProblems that no algorithm can solve
8.Hard

How do n! and 2ⁿ compare for large n?

  1. An! is O(n²)
  2. B2ⁿ grows faster than n!
  3. CThey grow at the same rate
  4. Dn! grows faster than 2ⁿ

Answer key

  1. 1. B · O(n)
  2. 2. A · 4 seconds
  3. 3. C · A case that is answered directly without making another recursive call
  4. 4. A · Merge sort
  5. 5. A · O(n)
  6. 6. A · O(n)
  7. 7. B · Decision problems whose yes-answers can be verified in polynomial time given a certificate
  8. 8. D · n! grows faster than 2ⁿ

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