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 O(n²) algorithm takes 1 second for n = 1000. About how long does it take for n = 2000?

  1. A1 second
  2. B2 seconds
  3. C8 seconds
  4. D4 seconds
2.Easy

How does selection sort work?

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

What does it mean if an operation runs in O(1) time?

  1. AIts running time does not grow with the input size
  2. BIt processes exactly one element per second of running time
  3. CIt always takes exactly one second
  4. DIt can only be run once
4.Medium

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

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

Which algorithm finds the shortest paths from one source in a graph with non-negative edge weights?

  1. ADijkstra's algorithm
  2. BDepth-first search
  3. CBellman–Ford is required in all cases
  4. DKruskal's algorithm
6.Medium

What does the recurrence T(n) = T(n − 1) + O(1) solve to?

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

Which of these satisfiability problems is known to be solvable in polynomial time?

  1. AGeneral SAT on formulas in conjunctive normal form
  2. B3-SAT, where each clause has exactly three literals
  3. C2-SAT, where each clause has at most two literals
  4. DCircuit satisfiability
8.Hard

For which knapsack variant does the greedy rule "take items in order of value per weight" always give an optimal answer?

  1. ANeither variant
  2. BThe 0/1 knapsack, where items must always be taken whole
  3. CBoth variants
  4. DThe fractional knapsack, where items may be split

Answer key

  1. 1. D · 4 seconds
  2. 2. C · It repeatedly finds the minimum of the unsorted part and swaps it to the front
  3. 3. A · Its running time does not grow with the input size
  4. 4. B · Its subproblems do not overlap, so there is nothing to reuse
  5. 5. A · Dijkstra's algorithm
  6. 6. D · O(n)
  7. 7. C · 2-SAT, where each clause has at most two literals
  8. 8. D · The fractional knapsack, where items may be split

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