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(log n)
  2. BO(2n)
  3. CO(n²)
  4. DO(n)
2.Easy

An algorithm's running time is O(n). What is this growth called?

  1. ALinear
  2. BConstant
  3. CQuadratic
  4. DLogarithmic
3.Easy

Which design strategy does merge sort use?

  1. AGreedy choice
  2. BBrute force
  3. CDivide and conquer
  4. DDynamic programming
4.Medium

Why do many library sorting routines switch to insertion sort for very small subarrays?

  1. ARecursive algorithms cannot handle arrays below 10 elements
  2. BInsertion sort is O(n log n) in the worst case
  3. CInsertion sort uses less memory than any other sorting algorithm on large inputs
  4. DInsertion sort has little overhead and is fast on small or nearly sorted inputs
5.Medium

What is the worst-case time complexity of quicksort?

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

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

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

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

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

The Held–Karp dynamic programming algorithm solves the travelling salesman problem exactly. What is its running time for n cities?

  1. AO(n³)
  2. BO(2n²)
  3. CO(n² 2ⁿ)
  4. DO(n!)

Answer key

  1. 1. C · O(n²)
  2. 2. A · Linear
  3. 3. C · Divide and conquer
  4. 4. D · Insertion sort has little overhead and is fast on small or nearly sorted inputs
  5. 5. B · O(n²)
  6. 6. B · O(n)
  7. 7. C · X is in NP, and a known NP-complete problem reduces to X in polynomial time
  8. 8. C · O(n² 2ⁿ)

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