Skip to content

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 T(n)=4T(n/2)+n2T(n) = 4T(n/2) + n^2 using the Master Theorem.

Solution:

Identify parameters: a=4a = 4, b=2b = 2, f(n)=n2f(n) = n^2

Compute logba=log24=2\log_b a = \log_2 4 = 2

Compare f(n)=n2f(n) = n^2 with nlogba=n2n^{\log_b a} = n^2:

Since f(n)=Θ(n2)=Θ(nlogba)f(n) = \Theta(n^2) = \Theta(n^{\log_b a}), we are in Case 2 of the Master Theorem.

T(n)=Θ(n2logn)T(n) = \Theta(n^2 \log n)

Verification: The recursion tree has log2n\log_2 n levels. At each level ii, there are 4i4^i subproblems of size n/2in/2^i, each costing (n/2i)2=n2/4i(n/2^i)^2 = n^2/4^i. Total work per level: 4i×n2/4i=n24^i \times n^2/4^i = n^2. Summing over log2n\log_2 n levels: T(n)=n2log2n=Θ(n2logn)T(n) = n^2 \log_2 n = \Theta(n^2 \log n). ✓


Example 2: Dynamic Programming — 0/1 Knapsack

Problem: Given items with weights [2,3,4,5][2, 3, 4, 5] and values [3,4,5,6][3, 4, 5, 6], capacity W=8W = 8, find the maximum value.

Solution:

DP Table Construction:

w=0w=0w=1w=1w=2w=2w=3w=3w=4w=4w=5w=5w=6w=6w=7w=7w=8w=8
0 items000000000
Item 1 (w=2, v=3)003333333
Item 2 (w=3, v=4)003447777
Item 3 (w=4, v=5)003457899
Item 4 (w=5, v=6)0034578910

Recurrence: dp[i][w]=max(dp[i1][w],dp[i1][wwi]+vi)dp[i][w] = \max(dp[i-1][w], dp[i-1][w-w_i] + v_i) if wiww_i \leq w

Answer: dp[4][8]=10dp[4][8] = 10 (items 2 and 4: weight 3+5=83+5=8, value 4+6=104+6=10)

Space-optimized version: Use 1D array of size W+1W+1, iterate items in outer loop, weights in reverse inner loop. Space: O(W)O(W) instead of O(nW)O(nW).


Example 3: Greedy — Activity Selection

Problem: Given activities with start and finish times: [(1,4),(3,5),(0,6),(5,7),(3,9),(5,9),(6,10),(8,11),(8,12),(2,14),(12,16)][(1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12), (2,14), (12,16)], 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.

ActivityStartFinishSelected?
(1,4)14
(3,5)35✗ (overlaps with (1,4))
(0,6)06✗ (overlaps with (1,4))
(5,7)57✓ (starts at 5 ≥ 4)
(3,9)39✗ (overlaps with (5,7))
(5,9)59✗ (overlaps with (5,7))
(6,10)610✗ (overlaps with (5,7))
(8,11)811✓ (starts at 8 ≥ 7)
(8,12)812✗ (overlaps with (8,11))
(2,14)214✗ (overlaps with (8,11))
(12,16)1216✓ (starts at 12 ≥ 11)

Maximum activities: 4 → {(1,4),(5,7),(8,11),(12,16)}\{(1,4), (5,7), (8,11), (12,16)\}

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).

See Also

Advanced Content

This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.

Derivations and Proofs

Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.

Extended Examples

Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.

Research Connections

This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.

Prerequisites

Ensure you have mastered the prerequisite material before attempting this advanced content.

Advanced Content

This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.

Derivations and Proofs

Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.

Extended Examples

Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.

Research Connections

This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.

Prerequisites

Ensure you have mastered the prerequisite material before attempting this advanced content.

Advanced Content

This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.

Derivations and Proofs

Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.

Extended Examples

Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.

Research Connections

This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.

Prerequisites

Ensure you have mastered the prerequisite material before attempting this advanced content.

Advanced Content

This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.

Derivations and Proofs

Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.

Extended Examples

Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.

Research Connections

This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.

Prerequisites

Ensure you have mastered the prerequisite material before attempting this advanced content.

Advanced Content

This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.

Derivations and Proofs

Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.

Extended Examples

Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.

Research Connections

This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.

Prerequisites

Ensure you have mastered the prerequisite material before attempting this advanced content.

Advanced Content

This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.

Derivations and Proofs

Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.

Extended Examples

Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.

Research Connections

This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.

Prerequisites

Ensure you have mastered the prerequisite material before attempting this advanced content.