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 a brute-force algorithm?

  1. AOne that tries all possible candidate solutions
  2. BOne that runs on many processors
  3. COne that always uses recursion
  4. DOne that guesses an answer at random
2.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. CIt processes exactly one element per second of running time
  4. DIts running time does not grow with the input size
3.Easy

An algorithm's running time is O(n). What is this growth called?

  1. ALogarithmic
  2. BLinear
  3. CQuadratic
  4. DConstant
4.Medium

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

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

A greedy algorithm:

  1. AMakes the locally best choice at each step
  2. BUses only recursion
  3. CAlways gives the optimal solution
  4. DTries every possible solution
6.Medium

What does amortised analysis measure?

  1. AThe expected cost over random inputs
  2. BThe average cost per operation over a worst-case sequence of operations
  3. CThe cost of an algorithm on the best input
  4. DThe cost of the single most expensive operation anywhere in the sequence
7.Hard

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

  1. AO(n²)
  2. BO(n)
  3. CO(n √n)
  4. DO(n log n)
8.Hard

A problem is NP-complete if it is:

  1. AIn NP and every NP problem reduces to it in polynomial time
  2. BSolvable in polynomial time
  3. CImpossible to solve
  4. DOutside NP but harder than all NP problems, such as the halting problem

Answer key

  1. 1. A · One that tries all possible candidate solutions
  2. 2. D · Its running time does not grow with the input size
  3. 3. B · Linear
  4. 4. A · O(log n)
  5. 5. A · Makes the locally best choice at each step
  6. 6. B · The average cost per operation over a worst-case sequence of operations
  7. 7. D · O(n log n)
  8. 8. A · In NP and every NP problem reduces to it in polynomial time

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