Code

Height, checking, top-k — write them yourself

The heap works: Push and Pop keep the property. The remaining three come with no holes this time — empty skeletons that you fill in completely.

Height and IsHeap

Two simple ones. The first:

Height():
    how many times n can be halved before it reaches 0
    // = floor(log2 n) + 1

The shape follows from the length alone — there is nothing to walk. In lesson 11 this meant visiting the whole tree.

IsHeap():
    for each i from 1:
        if a[(i−1)/2] > a[i]: return false
    return true

Simple and correct beats clever: the test calls it after every Push.

TopKHeap — where the thinking is

TopKHeap uses a min-heap of the k best items, and that is the whole trick.

The heap holds the k best seen so far. Its minimum is the weakest of those k. A new record only has to beat that one:

if the heap holds fewer than k: Push and continue
worst ← Peek()
c.Hit()
if the new one ≤ worst: skip        // cannot displace the weakest of the k best
Pop(); Push(the new one)

For most records that is one comparison and done. Which is why the cost is O(n log k) rather than O(n log n).

A min-heap for "the best" looks inverted, and it should: you keep the best and watch the worst of them, because that is the one that leaves first.

At the end the heap is drained and the result reversed — Pop gives the smallest first, and you want the best first.

Your job

heap.go (in the panel) has grown: three new skeletons, Height, IsHeap and TopKHeap, each with panic("not implemented").

Pop is still shown with its holes — the panel shows the skeleton, not your file. In your copy they are already filled in; leave them that way.

Read the contracts, not just the signatures — the doc comments say everything the test will check. TopKHeap especially: it does not sort the input and never holds more than k elements.

heap_test.go has grown too: five new tests, while the first two are unchanged.

The tests

test what it requires
TestTraceReproducesTheLessonTable (last step) the same table
TestPopKeepsTheHeapAndComesOutAscending (last step) the property after every pop
TestPopComesOutAscending the property holds after every Push (via IsHeap); draining gives ascending order
TestHeightIsImpliedByLength Height() is floor(log2 n)+1 for every n up to 5,000
TestTheHeapIsNotSorted the array is NOT sorted — and is still a heap
TestTopKAgreesWithSortingAndCostsLess the same k ratings, in under 2n comparisons
TestTopKLosesWhenKApproachesN at k = n the heap loses to sorting

Two of them demand that something is not the case, and both protect the point of the lesson.

"Not sorted" guards against an improvement that would ruin everything. Turn Push into an insertion sort and every other test still passes, while the cost goes from O(log n) to O(n). A heap does not sort — that is not a shortcoming, it is exactly what it sells in exchange for the speed.

"Loses at k = n" is the honest boundary. The heap wins only while k is well under n; step 6 measures where it turns over. The test stops that being quietly left out.

Run go test ./.... While you write, the test tells you which function and where it falls short.