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 worst-case time complexity of insertion sort?

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

What is a brute-force algorithm?

  1. AOne that always uses recursion
  2. BOne that runs on many processors
  3. COne that tries all possible candidate solutions
  4. DOne that guesses an answer at random
3.Easy

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

  1. A2 seconds
  2. B4 seconds
  3. C8 seconds
  4. D1 second
4.Medium

Which statement about a recursive function without a reachable base case is correct?

  1. AIt keeps calling itself until the call stack overflows
  2. BIt returns 0 automatically
  3. CIt runs in O(1) time
  4. DThe compiler always turns it into a loop that ends safely
5.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. BDynamic programming only works on graph problems, not on arrays
  3. CMerge sort has no recursive structure
  4. DMerge sort is already O(n)
6.Medium

Which algorithm finds the shortest paths from one source in a graph with non-negative edge weights?

  1. ADijkstra's algorithm
  2. BKruskal's algorithm
  3. CDepth-first search
  4. DBellman–Ford is required in all cases
7.Hard

Is 22n in O(2ⁿ)?

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

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

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

Answer key

  1. 1. B · O(n²)
  2. 2. C · One that tries all possible candidate solutions
  3. 3. B · 4 seconds
  4. 4. A · It keeps calling itself until the call stack overflows
  5. 5. A · Its subproblems do not overlap, so there is nothing to reuse
  6. 6. A · Dijkstra's algorithm
  7. 7. A · No, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound
  8. 8. C · n! grows faster than 2ⁿ

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