Okay, here's the revised exam with a few simpler introductory questions and some more mathematically challenging problems interspersed. I've also adjusted point values slightly to reflect the added complexity.
Algorithms & Data Structures Final Exam
Total: 100 points
Time: 2.5 hours
Section 1: Foundations & Quick Checks (15 points)
- (3 points) What is the difference between time complexity and space complexity? Give an example of an algorithm with O(n) time complexity and O(1) space complexity.
- (4 points) Explain the difference between a stack and a queue. Provide a real-world example of where each data structure is commonly used.
- (4 points) What is the purpose of recursion? What are the potential drawbacks of using recursion?
- (4 points) Define the term "Big O" notation. What does O(n2) mean in terms of algorithm performance?
Section 2: Algorithm Analysis & Asymptotics (18 points)
a) Given the recurrence:
𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑛 log 𝑛
Solve using the Master Theorem or recursion trees. Justify your answer. (6 points)
b) Compare 𝑂(𝑛!), Ω(2𝑛), and Θ(𝑛3) for large 𝑛. Provide real-world examples where each might occur. (6 points)
c) Prove that if f(n) = an2 + bn + c, then f(n) is O(n2). (6 points)
Section 3: Data Structure Design (15 points)
Design an efficient data structure for a ride-sharing app that supports:
insert(driverID, rating)
get_worst_driver()
update_rating(driverID, newRating)
a) Specify your ADT and invariants. (3 points)
b) Justify choice of underlying structures with complexity analysis. (6 points)
c) Pseudocode for update_rating and get_worst_driver. (6 points)
Section 4: Graph Algorithms (20 points)
Graph: Cities A-F connected by roads with travel times:
A-B(4), A-C(2), B-C(1), B-D(5), C-D(8), C-E(10), D-E(2), D-F(6), E-F(3)
a) Compute shortest paths from A using Dijkstra. Show:
- Priority queue state at each step
- Final distances/paths (8 points)
b) COVID now now closes vertex D. Modify your solution to avoid D. (4 points)
c) Prove why Dijkstra fails with negative weights using this graph. (6 points)
Section 5: Divide & Conquer & Dynamic Programming (20 points)
a) Design an 𝑂(𝑛) algorithm to find the 𝑘𝑡ℎ smallest element in an unsorted array. (6 points)
b) Derive worst-case complexity rigorously. (4 points)
No pseudocode – describe intuition and partitioning strategy.
c) Problem: Maximize profit from stock prices [𝑝₁, 𝑝₂, ..., 𝑝ₙ] with:
- At most two transactions
- No overlapping transactions
i) Define subproblems and recurrence. (6 points)
ii) Pseudocode with 𝑂(𝑛) time/space. (6 points)
iii) Apply to [3, 5, 1, 4, 6, 2]. Show DP table. (3 points)
Section 6: Hashing & Advanced Trees (15 points)
a) Keys: {22, 41, 18, 44, 59, 32, 31, 73}
Hash function: ℎ(𝑘) = 𝑘 mod 13
i) Insert using double hashing (ℎ₂(𝑘)=7−(𝑘 mod 7)). Show probes. (5 points)
ii) Resize table to size 19. Describe rehashing process and challenges. (5 points)
b) Insert [25, 30, 20, 28, 23, 10] into an empty AVL tree. Show:
- Tree after each insertion
- Rotation types (6 points)
Section 7: Real-World Systems & Bonus (10 points)
a) Explain how Bloom Filters reduce disk I/O in databases. Include:
- Parameters affecting false positives
- Space/computation tradeoffs (5 points)
b) Why might QuickSort be preferred over HeapSort for in-memory sorting? (5 points)
Bonus: Lower Bounds (5 points)
Prove that comparison-based sorting requires Ω(𝑛 log 𝑛) time. Use decision trees.
Grading Notes:
- Total: 100 points
- Show all work for partial credit
- Time limit: 2.5 hours
- Closed book, one page of handwritten notes allowed
Key Changes & Rationale:
- Introductory Questions: The first section provides a quick check of fundamental concepts.
- Mathematical Rigor: Added a proof question (Section 2c) to assess mathematical understanding.
- More Challenging DP: The DP problem is more complex, requiring careful recurrence definition and implementation.
- Advanced Tree Insertion: AVL tree insertion with rotations is a more involved task.
- Real-World Depth: Bloom Filters and QuickSort vs. HeapSort require deeper understanding of system-level considerations.
- Bonus Question: The lower bound proof is a classic theoretical question.
- Point Value Adjustments: Point values are adjusted to reflect the increased difficulty of the questions.
This revised exam aims to assess not only knowledge of algorithms and data structures but also the ability to apply them to solve problems, analyze their performance, and understand their tradeoffs in real-world scenarios.