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

Which of these running times is polynomial?

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

Which complexity class grows fastest for large n?

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

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

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

A greedy algorithm:

  1. AAlways gives the optimal solution
  2. BUses only recursion
  3. CMakes the locally best choice at each step
  4. DTries every possible solution
5.Medium

Which sorting algorithm is stable?

  1. AQuicksort (typical in-place version)
  2. BSelection sort (typical version)
  3. CHeap sort
  4. DMerge sort
6.Medium

A recurrence T(n) = 2T(n/2) + O(n) solves to:

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

The halting problem shows that:

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

How does the Bellman–Ford algorithm detect a negative-weight cycle reachable from the source?

  1. AAfter V − 1 rounds, it checks whether any edge can still be relaxed
  2. BIt counts the cycles with depth-first search
  3. CIt runs Dijkstra's algorithm twice and compares the two distance results
  4. DIt checks whether any edge has a negative weight

Answer key

  1. 1. D · O(n³)
  2. 2. C · O(2ⁿ)
  3. 3. A · O(n²)
  4. 4. C · Makes the locally best choice at each step
  5. 5. D · Merge sort
  6. 6. A · O(n log n)
  7. 7. C · No algorithm can decide for every program and input whether it halts
  8. 8. A · After V − 1 rounds, it checks whether any edge can still be relaxed

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