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

Which of these running times is polynomial?

  1. AO(n³)
  2. BO(nⁿ)
  3. CO(2ⁿ)
  4. DO(n!)
2.Easy

What is the best-case time complexity of insertion sort, on an already sorted array?

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

What is an algorithm?

  1. AA programming language designed for writing mathematical formulas
  2. BA finite sequence of well-defined steps that solves a problem
  3. CAny computer program that has a graphical interface
  4. DA data structure that stores numbers
4.Medium

Why does dynamic programming give no real speed-up for merge sort?

  1. ADynamic programming only works on graph problems, not on arrays
  2. BMerge sort is already O(n)
  3. CMerge sort has no recursive structure
  4. DIts subproblems do not overlap, so there is nothing to reuse
5.Medium

What does the recurrence T(n) = T(n − 1) + O(n) solve to?

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

A greedy algorithm:

  1. AMakes the locally best choice at each step
  2. BAlways gives the optimal solution
  3. CTries every possible solution
  4. DUses only recursion
7.Hard

Insertion sort runs in O(n) on sorted input. Does this contradict the Ω(n log n) lower bound for comparison sorting?

  1. ANo, because insertion sort is not a comparison sort
  2. BYes, but only for arrays shorter than 100
  3. CYes, the lower bound is therefore wrong
  4. DNo, the lower bound applies to the worst case over all inputs, not to every input
8.Hard

Which technique gives an expected O(n log n) running time for quicksort on any input?

  1. AUsing recursion only on the left side
  2. BChoosing the pivot uniformly at random
  3. CSorting the input before running quicksort
  4. DAlways choosing the first element

Answer key

  1. 1. A · O(n³)
  2. 2. B · O(n)
  3. 3. B · A finite sequence of well-defined steps that solves a problem
  4. 4. D · Its subproblems do not overlap, so there is nothing to reuse
  5. 5. D · O(n²)
  6. 6. A · Makes the locally best choice at each step
  7. 7. D · No, the lower bound applies to the worst case over all inputs, not to every input
  8. 8. B · Choosing the pivot uniformly at random

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