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 binary search on a sorted array?

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

What is the time complexity of linear search in an unsorted array of n elements (worst case)?

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

Which notation describes an upper bound on growth?

  1. ALittle omega
  2. BBig Theta
  3. CBig Omega
  4. DBig O
4.Medium

A function runs one loop of n iterations and then, separately, another loop of n iterations. What is its time complexity?

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

Which statement about a recursive function without a reachable base case is correct?

  1. AIt runs in O(1) time
  2. BIt returns 0 automatically
  3. CThe compiler always turns it into a loop that ends safely
  4. DIt keeps calling itself until the call stack overflows
6.Medium

Why is the base of a logarithm usually left out in Big O, as in O(log n)?

  1. ALogarithms are always rounded to integers
  2. BThe base only matters for small n
  3. CLogarithms with different bases differ only by a constant factor
  4. DAll logarithms in computer science have base 10, so it is implied
7.Hard

The halting problem shows that:

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

Shortest simple paths can be found in polynomial time. What is known about finding the longest simple path between two vertices in a general graph?

  1. AIt is undecidable
  2. BIt can be solved by Dijkstra's algorithm with negated weights
  3. CIt is always solvable in O(V + E)
  4. DIt is NP-hard

Answer key

  1. 1. B · O(log n)
  2. 2. B · O(n)
  3. 3. D · Big O
  4. 4. A · O(n)
  5. 5. D · It keeps calling itself until the call stack overflows
  6. 6. C · Logarithms with different bases differ only by a constant factor
  7. 7. C · No algorithm can decide for every program and input whether it halts
  8. 8. D · It is NP-hard

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