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 linear search in an unsorted array of n elements (worst case)?

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

What is a brute-force algorithm?

  1. AOne that runs on many processors
  2. BOne that always uses recursion
  3. COne that tries all possible candidate solutions
  4. DOne that guesses an answer at random
3.Easy

Which notation describes an upper bound on growth?

  1. ABig Theta
  2. BBig O
  3. CLittle omega
  4. DBig Omega
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

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 n³ is smaller than n² for large n
  3. CYes, because Big O is an upper bound and n² grows no faster than n³
  4. DNo, because Big O must always state the exact growth rate of the function
6.Medium

What is the amortised time complexity of appending to a dynamic array that doubles its capacity when full?

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

Which two things must be shown to prove that a decision problem X is NP-complete?

  1. AX is in P, and X reduces to SAT
  2. BX reduces to a problem in P, and X is in NP
  3. CX is in NP, and a known NP-complete problem reduces to X in polynomial time
  4. DX has no polynomial algorithm, and X is decidable
8.Hard

What is the time complexity of building a binary heap from n unsorted elements with the bottom-up method?

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

Answer key

  1. 1. B · O(n)
  2. 2. C · One that tries all possible candidate solutions
  3. 3. B · Big O
  4. 4. B · O(n log n)
  5. 5. C · Yes, because Big O is an upper bound and n² grows no faster than n³
  6. 6. A · O(1)
  7. 7. C · X is in NP, and a known NP-complete problem reduces to X in polynomial time
  8. 8. D · O(n)

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