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 notation describes an upper bound on growth?

  1. ABig Theta
  2. BLittle omega
  3. CBig Omega
  4. DBig O
2.Easy

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

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

What is recursion?

  1. AA loop that never ends
  2. BA type of data structure
  3. CA function that calls itself on smaller inputs
  4. DA sorting method
4.Medium

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

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

A greedy algorithm:

  1. AMakes the locally best choice at each step
  2. BTries every possible solution
  3. CUses only recursion
  4. DAlways gives the optimal solution
6.Medium

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

  1. AAn array whose elements are all different
  2. BAn array in random order
  3. CAn array that is already sorted
  4. DAn array of length 2
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, the lower bound applies to the worst case over all inputs, not to every input
  2. BYes, the lower bound is therefore wrong
  3. CNo, because insertion sort is not a comparison sort
  4. DYes, but only for arrays shorter than 100
8.Hard

Why can Dijkstra's algorithm give wrong results when some edge weights are negative?

  1. AA vertex is finalised when extracted, but a later negative edge could still give it a shorter path
  2. BIt goes into an infinite loop on every negative edge
  3. CNegative numbers cannot be stored in a priority queue
  4. DIt only works on undirected graphs

Answer key

  1. 1. D · Big O
  2. 2. D · O(n)
  3. 3. C · A function that calls itself on smaller inputs
  4. 4. D · O(n²)
  5. 5. A · Makes the locally best choice at each step
  6. 6. C · An array that is already sorted
  7. 7. A · No, the lower bound applies to the worst case over all inputs, not to every input
  8. 8. A · A vertex is finalised when extracted, but a later negative edge could still give it a shorter path

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