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

A problem is usually called tractable if:

  1. AIt has no solution
  2. BIt can be solved in polynomial time
  3. CIt can only be solved by trying every possibility
  4. DIt can be solved in constant time
2.Easy

Accessing an element by index in an array takes:

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

What is the time complexity of finding the largest value in an unsorted array of n numbers?

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

Is it correct to say that an O(n²) algorithm is also O(n³)?

  1. AYes, but only if n is smaller than 100
  2. BNo, because Big O must always state the exact growth rate of the function
  3. CNo, because n³ is smaller than n² for large n
  4. DYes, because Big O is an upper bound and n² grows no faster than n³
5.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)
6.Medium

The time complexity of naive recursive Fibonacci (fib(n) = fib(n−1) + fib(n−2)) is:

  1. AExponential
  2. BLinear
  3. CLogarithmic
  4. DConstant
7.Hard

What is the running time of Dijkstra's algorithm with a binary heap on a graph with V vertices and E edges?

  1. AO(V + E)
  2. BO(V³)
  3. CO((V + E) log V)
  4. DO(V × E)
8.Hard

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

  1. AQuickselect runs in O(log n) worst case, because it recurses on only one side of the pivot
  2. BQuickselect runs in expected O(n), and median-of-medians guarantees O(n) worst case
  3. CSelection always needs Ω(n log n) because it requires sorting
  4. DSelection is only possible in O(n²)

Answer key

  1. 1. B · It can be solved in polynomial time
  2. 2. D · O(1)
  3. 3. C · O(n)
  4. 4. D · Yes, because Big O is an upper bound and n² grows no faster than n³
  5. 5. A · O(2ⁿ)
  6. 6. A · Exponential
  7. 7. C · O((V + E) log V)
  8. 8. B · Quickselect runs in expected O(n), and median-of-medians guarantees O(n) worst case

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