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 time complexity of finding the largest value in an unsorted array of n numbers?

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

Which sorting algorithm repeatedly swaps adjacent elements that are in the wrong order?

  1. ABubble sort
  2. BHeap sort
  3. CMerge sort
  4. DQuicksort
3.Easy

What is log₂ 1024?

  1. A32
  2. B12
  3. C10
  4. D512
4.Medium

Why does dynamic programming give no real speed-up for merge sort?

  1. AMerge sort has no recursive structure
  2. BDynamic programming only works on graph problems, not on arrays
  3. CIts subproblems do not overlap, so there is nothing to reuse
  4. DMerge sort is already O(n)
5.Medium

What does it mean that a problem has optimal substructure?

  1. AEvery subproblem has the same size
  2. BThe problem has exactly one optimal solution, which every subproblem shares
  3. CAn optimal solution can be built from optimal solutions of its subproblems
  4. DThe problem can be solved in constant time
6.Medium

How much stack space does a simple recursive factorial function fact(n) = n × fact(n − 1) use?

  1. AO(1)
  2. BO(n)
  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. AKruskal's algorithm
  2. BFloyd–Warshall
  3. CDijkstra's algorithm
  4. DPrim's algorithm starting from one node
8.Hard

Is 22n in O(2ⁿ)?

  1. AYes, because both are exponential
  2. BNo, because 22n grows more slowly than 2ⁿ
  3. CYes, because constant factors in the exponent are always ignored in Big O
  4. DNo, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound

Answer key

  1. 1. A · O(n)
  2. 2. A · Bubble sort
  3. 3. C · 10
  4. 4. C · Its subproblems do not overlap, so there is nothing to reuse
  5. 5. C · An optimal solution can be built from optimal solutions of its subproblems
  6. 6. B · O(n)
  7. 7. A · Kruskal's algorithm
  8. 8. D · No, because 22n = 4ⁿ and 4ⁿ / 2ⁿ = 2ⁿ grows without bound

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