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

An algorithm's running time is O(n). What is this growth called?

  1. AQuadratic
  2. BLinear
  3. CLogarithmic
  4. DConstant
2.Easy

What does the worst-case running time of an algorithm describe?

  1. AThe expected time it takes on a random input of size n
  2. BThe time it takes on the smallest input
  3. CThe maximum time it takes over all inputs of size n
  4. DThe time it takes on a slow computer
3.Easy

How does selection sort work?

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

How does Prim's algorithm build a minimum spanning tree?

  1. AIt grows one tree from a start vertex, adding the cheapest edge that leaves the tree
  2. BIt runs breadth-first search and keeps the BFS tree
  3. CIt sorts all edges by weight and adds each one unless it would form a cycle in the forest
  4. DIt removes the most expensive edge until no cycles remain
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

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

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

Which definition of the class P is correct?

  1. AProblems that need exponential time on every computer
  2. BProblems that can be solved in parallel
  3. CProblems whose solutions can be checked but never found in polynomial time
  4. DDecision problems solvable by a deterministic algorithm in polynomial time
8.Hard

What is the running time of Dijkstra's algorithm with a binary heap on a graph with V vertices and E edges?

  1. AO(V × E)
  2. BO(V³)
  3. CO((V + E) log V)
  4. DO(V + E)

Answer key

  1. 1. B · Linear
  2. 2. C · The maximum time it takes over all inputs of size n
  3. 3. A · It repeatedly finds the minimum of the unsorted part and swaps it to the front
  4. 4. A · It grows one tree from a start vertex, adding the cheapest edge that leaves the tree
  5. 5. B · O(n²)
  6. 6. A · Shortest paths between all pairs of vertices in O(V³)
  7. 7. D · Decision problems solvable by a deterministic algorithm in polynomial time
  8. 8. C · O((V + E) log V)

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