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

Which sorting algorithm chooses a pivot and partitions the array into smaller and larger elements?

  1. ABubble sort
  2. BMerge sort
  3. CInsertion sort
  4. DQuicksort
2.Easy

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

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

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

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

Is it correct to say that an O(n²) algorithm is also O(n³)?

  1. AYes, but only if n is smaller than 100
  2. BYes, because Big O is an upper bound and n² grows no faster than n³
  3. CNo, because n³ is smaller than n² for large n
  4. DNo, because Big O must always state the exact growth rate of the function
5.Medium

What is the time complexity of the standard dynamic programming algorithm for the longest common subsequence of strings of lengths m and n?

  1. AO(2m+n)
  2. BO(mn log n)
  3. CO(mn)
  4. DO(m + n)
6.Medium

You sort an array of n elements once and then answer k membership queries with binary search. What is the total cost?

  1. AO(n + k)
  2. BO(n log n + k log n)
  3. CO(k n log n)
  4. DO(nk)
7.Hard

The halting problem shows that:

  1. ALoops should be avoided
  2. BEvery program eventually halts
  3. CSome programs run faster on faster computers, so halting depends on hardware
  4. DNo algorithm can decide for every program and input whether it halts
8.Hard

Strassen's algorithm multiplies matrices using 7 instead of 8 recursive multiplications of half-size blocks. What is its running time?

  1. AΘ(n³)
  2. BΘ(n² log n)
  3. CΘ(n2)
  4. DΘ(nlog₂ 7), about n2.81

Answer key

  1. 1. D · Quicksort
  2. 2. D · O(V + E)
  3. 3. B · O(n²)
  4. 4. B · Yes, because Big O is an upper bound and n² grows no faster than n³
  5. 5. C · O(mn)
  6. 6. B · O(n log n + k log n)
  7. 7. D · No algorithm can decide for every program and input whether it halts
  8. 8. D · Θ(nlog₂ 7), about n2.81

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