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

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
2.Easy

Which complexity class grows fastest for large n?

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

What is the worst-case time complexity of merge sort?

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

The lower bound for comparison-based sorting in the worst case is:

  1. AΩ(n²)
  2. BΩ(log n)
  3. CΩ(n log n)
  4. DΩ(n)
5.Medium

What is the time complexity of the Bellman–Ford algorithm on a graph with V vertices and E edges?

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

What is the amortised time complexity of appending to a dynamic array that doubles its capacity when full?

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

Which algorithm finds a minimum spanning tree by repeatedly adding the cheapest edge that does not form a cycle?

  1. AFloyd–Warshall
  2. BKruskal's algorithm
  3. CDijkstra's algorithm
  4. DPrim's algorithm starting from one node
8.Hard

What are the time and space costs of computing the n-th Fibonacci number with a loop that keeps only the last two values?

  1. AO(n) time and O(1) space
  2. BO(2ⁿ) time and O(1) space
  3. CO(log n) time and O(n) space
  4. DO(n) time and O(n) space

Answer key

  1. 1. C · A case that is answered directly without making another recursive call
  2. 2. C · O(2ⁿ)
  3. 3. C · O(n log n)
  4. 4. C · Ω(n log n)
  5. 5. A · O(V × E)
  6. 6. B · O(1)
  7. 7. B · Kruskal's algorithm
  8. 8. A · O(n) time and O(1) space

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