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 log₂ 1024?

  1. A10
  2. B32
  3. C12
  4. D512
2.Easy

Which of these grows slowest as n becomes large?

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

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

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

In a sorted array, you look for two elements that sum to a target by moving one pointer from each end inwards. What is the time complexity?

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

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

  1. ALinear
  2. BLogarithmic
  3. CExponential
  4. DConstant
7.Hard

Why does binary search not give O(log n) time on a singly linked list?

  1. AComparisons on linked list nodes are slower
  2. BLinked lists cannot be sorted
  3. CBinary search needs at least twice the memory
  4. DReaching the middle element takes O(n) steps, since there is no direct index access
8.Hard

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

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

Answer key

  1. 1. A · 10
  2. 2. D · O(log n)
  3. 3. B · O(n)
  4. 4. B · O(n log n)
  5. 5. D · O(n)
  6. 6. C · Exponential
  7. 7. D · Reaching the middle element takes O(n) steps, since there is no direct index access
  8. 8. C · Many keys may collide into the same bucket

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