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 time complexity of finding the largest value in an unsorted array of n numbers?

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

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

  1. ASort, search, return
  2. BRead, compute, write
  3. CDivide, conquer, combine
  4. DGuess, check, repeat
3.Easy

Accessing an element by index in an array takes:

  1. AO(1)
  2. BO(log n)
  3. CO(n)
  4. DO(n²)
4.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. BMerge sort is already O(n)
  3. CDynamic programming only works on graph problems, not on arrays
  4. DMerge sort has no recursive structure
5.Medium

What does the Floyd–Warshall algorithm compute, and how fast?

  1. AA minimum spanning tree in O(E log V)
  2. BShortest paths between all pairs of vertices in O(V³)
  3. CShortest paths from one source vertex to all others in O(V + E)
  4. DA topological order in O(V²)
6.Medium

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

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

Which statement about selecting the k-th smallest element of an unsorted array is correct?

  1. ASelection is only possible in O(n²)
  2. BSelection always needs Ω(n log n) because it requires sorting
  3. CQuickselect runs in O(log n) worst case, because it recurses on only one side of the pivot
  4. DQuickselect runs in expected O(n), and median-of-medians guarantees O(n) worst case
8.Hard

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

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

Answer key

  1. 1. C · O(n)
  2. 2. C · Divide, conquer, combine
  3. 3. A · O(1)
  4. 4. A · Its subproblems do not overlap, so there is nothing to reuse
  5. 5. B · Shortest paths between all pairs of vertices in O(V³)
  6. 6. D · O(n)
  7. 7. D · Quickselect runs in expected O(n), and median-of-medians guarantees O(n) worst case
  8. 8. A · n! grows faster than 2ⁿ

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