Algorithms
Source: Concepts adapted from Ace the Data Science Interview — Chapter 9 (“Coding”) by Kevin Huo & Nick Singh. Rewritten in original wording for this course.
Recursion, greedy, and dynamic programming (especially knapsack) show up constantly. Sorting gets its own deep dive in Sorting algorithms with step animations and implement-all-the-sorts labs.
Recursion vs iteration
A recursive function needs a base case and a recursive case. Naïve Fibonacci recomputes the same fib(k) exponentially often and can blow the call stack. Iterative Fibonacci (or memoized recursion) is time.
Recursion (harder)
1 / 3Naïve recursive Fibonacci without memoization is typically…
Pick an answer to see if you are right.
Greedy algorithms
Pick the locally best option each step. Works for canonical coin systems; fails on 0/1 knapsack. Say the greedy-choice property out loud — or switch to DP.
Greedy (harder)
1 / 3Items (w,v)=(2,3),(3,4),(4,5) with W=5. Optimal 0/1 value is…
Pick an answer to see if you are right.
Dynamic programming & knapsack
Needs optimal substructure + overlapping subproblems. Top-down memo or bottom-up table.
For 0/1 knapsack with capacity and item weight , value :
Loop downward so each item is used at most once. Loop upward for unbounded/complete knapsack.
Step through the same items both ways:
Knapsack & DP (harder)
1 / 5Why does 0/1 knapsack iterate j from W down to wᵢ?
Pick an answer to see if you are right.
Written: knapsack tradeoffs
Using “downward” and “reuse”, explain:
Explain how the 1-D DP loop direction encodes 0/1 vs unbounded knapsack, and give a tiny numeric counterexample where ratio-greedy fails for 0/1.
Minimum 45 words. Your answer must include: “downward”, “reuse”.
Meet the writing requirements, then you can open the model answer.
Labs: knapsack variants
Other classic practice
Next: Sorting algorithms (implement ≥5 sorts + grid animations) · Prev: Data structures.