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 a brute-force algorithm?

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

What are the three steps of a divide-and-conquer algorithm?

  1. ARead, compute, write
  2. BDivide, conquer, combine
  3. CSort, search, return
  4. DGuess, check, repeat
3.Easy

How does selection sort work?

  1. AIt splits the array in halves and merges them
  2. BIt repeatedly swaps neighbouring elements that are in the wrong order
  3. CIt inserts each element into a growing sorted part by shifting
  4. DIt repeatedly finds the minimum of the unsorted part and swaps it to the front
4.Medium

What is the time complexity of: for i = 1 to n: (j = 1; while j < n: j = j × 2)?

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

Why is the base of a logarithm usually left out in Big O, as in O(log n)?

  1. AAll logarithms in computer science have base 10, so it is implied
  2. BThe base only matters for small n
  3. CLogarithms with different bases differ only by a constant factor
  4. DLogarithms are always rounded to integers
6.Medium

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

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

The halting problem shows that:

  1. AEvery program eventually halts
  2. BNo algorithm can decide for every program and input whether it halts
  3. CSome programs run faster on faster computers, so halting depends on hardware
  4. DLoops should be avoided
8.Hard

Is 22n in O(2ⁿ)?

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

Answer key

  1. 1. D · One that tries all possible candidate solutions
  2. 2. B · Divide, conquer, combine
  3. 3. D · It repeatedly finds the minimum of the unsorted part and swaps it to the front
  4. 4. C · O(n log n)
  5. 5. C · Logarithms with different bases differ only by a constant factor
  6. 6. A · O(2ⁿ)
  7. 7. B · No algorithm can decide for every program and input whether it halts
  8. 8. C · No, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound

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