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

A loop starts with i = 1 and doubles i each iteration until i > n. How many iterations does it run?

  1. AAbout n²
  2. BAbout n
  3. CAbout log₂ n
  4. DAbout n / 2
2.Easy

What is O(2ⁿ) growth called?

  1. ALinear
  2. BExponential
  3. CLogarithmic
  4. DPolynomial
3.Easy

What is the base case of a recursive function?

  1. AThe case with the largest input, which is handled before any recursive calls
  2. BThe first line of the function
  3. CA case that is answered directly without making another recursive call
  4. DThe call that uses the most memory
4.Medium

How many levels does the recursion of merge sort have for an array of n elements?

  1. AAbout n
  2. BAbout n / 2
  3. CAbout log₂ n
  4. DAbout √n
5.Medium

Breadth-first search on an unweighted graph finds:

  1. AThe minimum spanning tree
  2. BShortest paths in number of edges from the start node
  3. CShortest paths in graphs that contain negative edge weights
  4. DA topological order in all graphs
6.Medium

What does the Floyd–Warshall algorithm compute, and how fast?

  1. AA topological order in O(V²)
  2. BA minimum spanning tree in O(E log V)
  3. CShortest paths from one source vertex to all others in O(V + E)
  4. DShortest paths between all pairs of vertices in O(V³)
7.Hard

Topological sorting is possible only for:

  1. AGraphs with negative weights
  2. BComplete graphs
  3. CUndirected graphs
  4. DDirected acyclic graphs
8.Hard

How does the Bellman–Ford algorithm detect a negative-weight cycle reachable from the source?

  1. AIt counts the cycles with depth-first search
  2. BIt runs Dijkstra's algorithm twice and compares the two distance results
  3. CIt checks whether any edge has a negative weight
  4. DAfter V − 1 rounds, it checks whether any edge can still be relaxed

Answer key

  1. 1. C · About log₂ n
  2. 2. B · Exponential
  3. 3. C · A case that is answered directly without making another recursive call
  4. 4. C · About log₂ n
  5. 5. B · Shortest paths in number of edges from the start node
  6. 6. D · Shortest paths between all pairs of vertices in O(V³)
  7. 7. D · Directed acyclic graphs
  8. 8. D · After V − 1 rounds, it checks whether any edge can still be relaxed

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