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

How does selection sort work?

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

What is the best-case time complexity of insertion sort, on an already sorted array?

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

What is the time complexity of depth-first search on a graph stored as adjacency lists?

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

With coins of 1, 3 and 4 and an amount of 6, how many coins does the greedy "largest coin first" method use, compared to the optimum?

  1. AGreedy uses 2 coins; the optimum is 2
  2. BGreedy uses 3 coins; the optimum is 2
  3. CGreedy uses 4 coins; the optimum is 3
  4. DGreedy uses 3 coins; the optimum is 3
5.Medium

The time complexity of naive recursive Fibonacci (fib(n) = fib(n−1) + fib(n−2)) is:

  1. ALogarithmic
  2. BExponential
  3. CLinear
  4. DConstant
6.Medium

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

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

Topological sorting is possible only for:

  1. AUndirected graphs
  2. BGraphs with negative weights
  3. CDirected acyclic graphs
  4. DComplete graphs
8.Hard

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

  1. AFloyd–Warshall
  2. BPrim's algorithm starting from one node
  3. CDijkstra's algorithm
  4. DKruskal's algorithm

Answer key

  1. 1. A · It repeatedly finds the minimum of the unsorted part and swaps it to the front
  2. 2. D · O(n)
  3. 3. A · O(V + E)
  4. 4. B · Greedy uses 3 coins; the optimum is 2
  5. 5. B · Exponential
  6. 6. A · O(n)
  7. 7. C · Directed acyclic graphs
  8. 8. D · Kruskal's algorithm

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