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 base case of a recursive function?

  1. AThe call that uses the most memory
  2. BA case that is answered directly without making another recursive call
  3. CThe case with the largest input, which is handled before any recursive calls
  4. DThe first line of the function
2.Easy

An algorithm with two nested loops, each running n times, typically has which complexity?

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

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

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

What is the worst-case time complexity of quicksort?

  1. AO(log n)
  2. BO(n log n)
  3. CO(n)
  4. DO(n²)
5.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(k n log n)
  3. CO(n log n + k log n)
  4. DO(nk)
6.Medium

A greedy algorithm:

  1. AUses only recursion
  2. BAlways gives the optimal solution
  3. CMakes the locally best choice at each step
  4. DTries every possible solution
7.Hard

The halting problem shows that:

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

Why is the average lookup in a hash table O(1) but the worst case O(n)?

  1. AMany keys may collide into the same bucket
  2. BHash functions are slow
  3. CArrays have O(n) access
  4. DKeys must be sorted first

Answer key

  1. 1. B · A case that is answered directly without making another recursive call
  2. 2. C · O(n²)
  3. 3. C · Its running time does not grow with the input size
  4. 4. D · O(n²)
  5. 5. C · O(n log n + k log n)
  6. 6. C · Makes the locally best choice at each step
  7. 7. A · No algorithm can decide for every program and input whether it halts
  8. 8. A · Many keys may collide into the same bucket

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