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

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

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

What is log₂ 1024?

  1. A32
  2. B512
  3. C12
  4. D10
3.Easy

An O(n²) algorithm takes 1 second for n = 1000. About how long does it take for n = 2000?

  1. A4 seconds
  2. B2 seconds
  3. C8 seconds
  4. D1 second
4.Medium

What does amortised analysis measure?

  1. AThe average cost per operation over a worst-case sequence of operations
  2. BThe expected cost over random inputs
  3. CThe cost of the single most expensive operation anywhere in the sequence
  4. DThe cost of an algorithm on the best input
5.Medium

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

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

What is the time complexity of the standard dynamic programming algorithm for the longest common subsequence of strings of lengths m and n?

  1. AO(2m+n)
  2. BO(mn log n)
  3. CO(m + n)
  4. DO(mn)
7.Hard

What does the recurrence T(n) = 2T(n/2) + n log n solve to?

  1. AΘ(n)
  2. BΘ(n log n)
  3. CΘ(n²)
  4. DΘ(n log² n)
8.Hard

Why is the average lookup in a hash table O(1) but the worst case O(n)?

  1. AKeys must be sorted first
  2. BMany keys may collide into the same bucket
  3. CHash functions are slow
  4. DArrays have O(n) access

Answer key

  1. 1. B · O(n)
  2. 2. D · 10
  3. 3. A · 4 seconds
  4. 4. A · The average cost per operation over a worst-case sequence of operations
  5. 5. A · An array that is already sorted
  6. 6. D · O(mn)
  7. 7. D · Θ(n log² n)
  8. 8. B · Many keys may collide into the same bucket

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