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?
- AO(3n²)
- BO(5n)
- CO(n³)
- DO(n²)
2.Easy
An algorithm with two nested loops, each running n times, typically has which complexity?
- AO(n²)
- BO(log n)
- CO(n)
- DO(2n)
3.Easy
What does it mean if an operation runs in O(1) time?
- AIt always takes exactly one second
- BIt processes exactly one element per second of running time
- CIts running time does not grow with the input size
- DIt can only be run once
4.Medium
What does the Floyd–Warshall algorithm compute, and how fast?
- AA minimum spanning tree in O(E log V)
- BShortest paths between all pairs of vertices in O(V³)
- CShortest paths from one source vertex to all others in O(V + E)
- DA topological order in O(V²)
5.Medium
What does it mean that a problem has optimal substructure?
- AEvery subproblem has the same size
- BThe problem can be solved in constant time
- CAn optimal solution can be built from optimal solutions of its subproblems
- DThe problem has exactly one optimal solution, which every subproblem shares
6.Medium
What does the recurrence T(n) = T(n/2) + O(1) solve to?
- AO(n)
- BO(n log n)
- CO(1)
- DO(log n)
7.Hard
Which algorithm finds a minimum spanning tree by repeatedly adding the cheapest edge that does not form a cycle?
- AFloyd–Warshall
- BDijkstra's algorithm
- CKruskal's algorithm
- DPrim's algorithm starting from one node
8.Hard
Why can Dijkstra's algorithm give wrong results when some edge weights are negative?
- AIt goes into an infinite loop on every negative edge
- BIt only works on undirected graphs
- CNegative numbers cannot be stored in a priority queue
- DA vertex is finalised when extracted, but a later negative edge could still give it a shorter path