University CS Practice: Algorithms
University CS — Algorithms Practice Problems
10 MCQ questions covering algorithm design paradigms, complexity analysis, divide-and-conquer, dynamic programming, greedy algorithms, and amortized analysis. Select an option to check your answer and view the explanation.
Intuition
Different strategies for different problems: Algorithm design paradigms are like different strategies for solving a jigsaw puzzle, divide-and-conquer splits the puzzle into sections, dynamic programming remembers which pieces you’ve already tried, and greedy algorithms grab the most obvious piece first.
Why it matters: The right algorithm can mean the difference between a solution that takes seconds and one that takes centuries. Understanding these paradigms gives you a toolbox for approaching any computational problem.
The key insight: Greedy algorithms are fast but not always optimal, they work when local choices lead to global optima (like Huffman coding) but fail when they don’t (like the coin change problem with non-standard denominations).
Worked Examples
Example 1: Master Theorem, Solving Recurrences
Problem: Solve using the Master Theorem.
Solution:
Identify parameters: , ,
Compute
Compare with :
Since , we are in Case 2 of the Master Theorem.
Verification: The recursion tree has levels. At each level , there are subproblems of size , each costing . Total work per level: . Summing over levels: . ✓
Example 2: Dynamic Programming, 0/1 Knapsack
Problem: Given items with weights and values , capacity , find the maximum value.
Solution:
DP Table Construction:
| 0 items | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| Item 1 (w=2, v=3) | 0 | 0 | 3 | 3 | 3 | 3 | 3 | 3 | 3 |
| Item 2 (w=3, v=4) | 0 | 0 | 3 | 4 | 4 | 7 | 7 | 7 | 7 |
| Item 3 (w=4, v=5) | 0 | 0 | 3 | 4 | 5 | 7 | 8 | 9 | 9 |
| Item 4 (w=5, v=6) | 0 | 0 | 3 | 4 | 5 | 7 | 8 | 9 | 10 |
Recurrence: if
Answer: (items 2 and 4: weight , value )
Space-optimized version: Use 1D array of size , iterate items in outer loop, weights in reverse inner loop. Space: instead of .
Example 3: Greedy, Activity Selection
Problem: Given activities with start and finish times: , find the maximum number of non-overlapping activities.
Solution:
Greedy strategy: Sort by finish time, then select activities that don’t overlap with the last selected.
| Activity | Start | Finish | Selected? |
|---|---|---|---|
| (1,4) | 1 | 4 | ✓ |
| (3,5) | 3 | 5 | ✗ (overlaps with (1,4)) |
| (0,6) | 0 | 6 | ✗ (overlaps with (1,4)) |
| (5,7) | 5 | 7 | ✓ (starts at 5 ≥ 4) |
| (3,9) | 3 | 9 | ✗ (overlaps with (5,7)) |
| (5,9) | 5 | 9 | ✗ (overlaps with (5,7)) |
| (6,10) | 6 | 10 | ✗ (overlaps with (5,7)) |
| (8,11) | 8 | 11 | ✓ (starts at 8 ≥ 7) |
| (8,12) | 8 | 12 | ✗ (overlaps with (8,11)) |
| (2,14) | 2 | 14 | ✗ (overlaps with (8,11)) |
| (12,16) | 12 | 16 | ✓ (starts at 12 ≥ 11) |
Maximum activities: 4 →
Key insight: The greedy choice (earliest finish time) leaves maximum room for remaining activities. This is provably optimal by exchange argument.
Algorithm Design Paradigms and Complexity
Divide and Conquer and Dynamic Programming
Greedy Algorithms and Amortized Analysis
Common Mistakes
Confusing worst-case with amortised analysis: Worst-case is the cost of the single most expensive operation. Amortised is the average cost per operation over a sequence. Don’t confuse the two.
Assuming greedy algorithms always give optimal solutions: Greedy algorithms work for some problems (Huffman, MST) but fail for others (0/1 knapsack). Always check if greedy choice property holds.
Forgetting that Big-O is an upper bound: O(n) means the algorithm is at most linear. It doesn’t mean it’s exactly linear. Don’t confuse O(n) with Θ(n).