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 does selection sort work?

  1. AIt splits the array in halves and merges them
  2. BIt repeatedly finds the minimum of the unsorted part and swaps it to the front
  3. CIt inserts each element into a growing sorted part by shifting
  4. DIt repeatedly swaps neighbouring elements that are in the wrong order
2.Easy

Space complexity measures:

  1. AThe physical size of the computer
  2. BThe amount of memory an algorithm needs as input grows
  3. CThe disk space of the program file once it is compiled
  4. DThe number of lines of code
3.Easy

What is the time complexity of binary search on a sorted array?

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

Is it correct to say that an O(n²) algorithm is also O(n³)?

  1. ANo, because n³ is smaller than n² for large n
  2. BNo, because Big O must always state the exact growth rate of the function
  3. CYes, because Big O is an upper bound and n² grows no faster than n³
  4. DYes, but only if n is smaller than 100
5.Medium

A function runs one loop of n iterations and then, separately, another loop of n iterations. What is its time complexity?

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

Breadth-first search on an unweighted graph finds:

  1. AA topological order in all graphs
  2. BShortest paths in graphs that contain negative edge weights
  3. CShortest paths in number of edges from the start node
  4. DThe minimum spanning tree
7.Hard

The 0/1 knapsack problem solved with dynamic programming takes O(nW) time. Why is this called pseudo-polynomial?

  1. ABecause it only works for W = 0
  2. BW is a number, so the time is exponential in the number of bits used to write W
  3. CBecause the algorithm is randomised and its running time varies with each input
  4. DBecause n is always small
8.Hard

Strassen's algorithm multiplies matrices using 7 instead of 8 recursive multiplications of half-size blocks. What is its running time?

  1. AΘ(n² log n)
  2. BΘ(n³)
  3. CΘ(nlog₂ 7), about n2.81
  4. DΘ(n2)

Answer key

  1. 1. B · It repeatedly finds the minimum of the unsorted part and swaps it to the front
  2. 2. B · The amount of memory an algorithm needs as input grows
  3. 3. A · O(log n)
  4. 4. C · Yes, because Big O is an upper bound and n² grows no faster than n³
  5. 5. C · O(n)
  6. 6. C · Shortest paths in number of edges from the start node
  7. 7. B · W is a number, so the time is exponential in the number of bits used to write W
  8. 8. C · Θ(nlog₂ 7), about n2.81

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