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

An algorithm with two nested loops, each running n times, typically has which complexity?

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

What is the time complexity of finding the largest value in an unsorted array of n numbers?

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

What is the worst-case time complexity of insertion sort?

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

How much stack space does a simple recursive factorial function fact(n) = n × fact(n − 1) use?

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

The lower bound for comparison-based sorting in the worst case is:

  1. AΩ(n)
  2. BΩ(n log n)
  3. CΩ(n²)
  4. DΩ(log n)
6.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(k n log n)
  3. CO(n + k)
  4. DO(n log n + k log n)
7.Hard

Is 22n in O(2ⁿ)?

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

Which two things must be shown to prove that a decision problem X is NP-complete?

  1. AX is in NP, and a known NP-complete problem reduces to X in polynomial time
  2. BX reduces to a problem in P, and X is in NP
  3. CX has no polynomial algorithm, and X is decidable
  4. DX is in P, and X reduces to SAT

Answer key

  1. 1. B · O(n²)
  2. 2. A · O(n)
  3. 3. A · O(n²)
  4. 4. C · O(n)
  5. 5. B · Ω(n log n)
  6. 6. D · O(n log n + k log n)
  7. 7. D · No, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound
  8. 8. A · X is in NP, and a known NP-complete problem reduces to X in polynomial time

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