Homework
What to hand in
algo with top and pq, passing go test ./..., plus a report.
1. The abstraction rule, tested on a fourth case
Step 7 gave a rule and three measurements behind it. Find a fourth case in
your own algo and test the rule against it.
Candidates: sort.Slice (a closure) against sort.Sort (an interface) on the
same data; strings.Builder against +=; your LinearFind against
slices.IndexFunc.
Predict first, using the three questions in step 7. Then measure. Report the prediction even if it was wrong — especially then.
2. What the counter cannot see, collected
Five lessons have now found a boundary: 4, 6, 11, 12, 13.
For each, one line: what was measured, what the counter said, and what the clock said.
Then propose the rule for when to reach for the counter and when for the clock. It should be one sentence and it should cover all five.
3. The queue Dijkstra needs
Lessons 14 and 15 will ask your heap for "the nearest unvisited node". Your
MinHeap keys on Rating.
What has to change? Write down the interface Dijkstra will need — including one operation you have not implemented and will discover you need. (Hint: what happens when you find a shorter route to a node that is already in the queue?)
Do not implement it. Describe what it would cost, and name one way to avoid needing it at all.
4. The top-ten feature, end to end
Add algo chart to your library: the ten highest-rated books per decade.
That is ten separate top-k problems, one per decade. Two designs:
- one pass over the data, ten heaps kept side by side;
- ten passes, one heap each.
Predict which wins and by how much. Measure both. Explain the result in terms of what each design touches in memory — lesson 11 measured that same effect from the other direction.
5. When to build it yourself
You have now hand-written a list, a search, four sorts, a hash index, two trees and a heap. In exactly two of those cases the standard library was slower.
One paragraph: what would you actually reach for at work, and what would have to be true for you to write your own instead? Name the measurement you would take before deciding.
The answer is not "always the standard library" and not "always by hand". If your paragraph reads like either one, you have not used step 7's rule.