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(n²)
  2. BO(n log n)
  3. CO(log n)
  4. DO(n)
2.Easy

How much extra memory does a standard array-based merge sort use?

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

What is log₂ 1024?

  1. A512
  2. B32
  3. C10
  4. D12
4.Medium

The lower bound for comparison-based sorting in the worst case is:

  1. AΩ(n log n)
  2. BΩ(n)
  3. CΩ(n²)
  4. DΩ(log n)
5.Medium

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

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

Quicksort always uses the first element as pivot. On which input does it show its worst-case behaviour?

  1. AAn array of length 2
  2. BAn array whose elements are all different
  3. CAn array that is already sorted
  4. DAn array in random order
7.Hard

What is the time complexity of: for i = 1 to n: for (j = i; j ≤ n; j = j + i) do O(1) work?

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

Shortest simple paths can be found in polynomial time. What is known about finding the longest simple path between two vertices in a general graph?

  1. AIt can be solved by Dijkstra's algorithm with negated weights
  2. BIt is undecidable
  3. CIt is NP-hard
  4. DIt is always solvable in O(V + E)

Answer key

  1. 1. A · O(n²)
  2. 2. C · O(n)
  3. 3. C · 10
  4. 4. A · Ω(n log n)
  5. 5. C · Logarithms with different bases differ only by a constant factor
  6. 6. C · An array that is already sorted
  7. 7. D · O(n log n)
  8. 8. C · It is NP-hard

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