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 Big O of 3n² + 5n + 2?

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

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

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

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

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

What is the worst-case time complexity of quicksort?

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

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

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

What is the time complexity of: for i = 1 to n: for j = 1 to i: do O(1) work?

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

Why does binary search not give O(log n) time on a singly linked list?

  1. ALinked lists cannot be sorted
  2. BReaching the middle element takes O(n) steps, since there is no direct index access
  3. CComparisons on linked list nodes are slower
  4. DBinary search needs at least twice the memory
8.Hard

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

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

Answer key

  1. 1. A · O(n²)
  2. 2. C · The maximum time it takes over all inputs of size n
  3. 3. D · O(V + E)
  4. 4. C · O(n²)
  5. 5. A · O(n)
  6. 6. D · O(n²)
  7. 7. B · Reaching the middle element takes O(n) steps, since there is no direct index access
  8. 8. B · The fractional knapsack, where items may be split

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