What must be easy to discard?

Retain the best three scores. Current scores are [5,2,8]; next score is 6.

Decision: Which retained value must be exposed, and why does a maximum at the root not answer the replacement question?

1 / 10

Kth Largest Element in an Array — predict before coding

Keep the best k values seen so far and expose the weakest retained one. A min-heap makes that threshold available at the root; duplicates occupy separate ranks.

Decision: A stream summary keeps only the largest two occurrences. Prefixes [1,10,9] and [8,10,9] produce the same summary. If a later query asks for the third-largest value of that past prefix, can the stored state answer it?

2 / 10

Top K Frequent Elements — predict before coding

First count occurrences, then rank distinct values by their counts. The heap entries are values with frequencies, not individual occurrences.

Decision: A streaming shortcut evicts low-frequency values before the input ends and then ignores their future occurrences. With k=1, it sees [2,1,1], keeps 2 after the first tie, and permanently discards 1. Why is final frequency counting different?

3 / 10

Find K Pairs with Smallest Sums — predict before coding

Each first-array index defines a sorted row of pair sums as the second index increases. The heap merges these sorted rows by keeping only the next unreported pair from each active row.

Decision: A different pair-grid search expands both (i+1,j) and (i,j+1) after every pop. Starting at (0,0), show two ways to schedule (1,1). Why does the reference’s seeded-row successor rule need no visited set?

4 / 10

Pair sums: a frontier trace

A=[1,4,7,10], B=[2,3], k=3.

Pop Reveal Next heap sums
(0,0), sum 3 (0,1), sum 4 4,6,9
(0,1), sum 4 row 0 exhausted 6,9
(1,0), sum 6 (1,1), sum 7 7,9

Each index pair has one predecessor in its row.

5 / 10

Pair sums: why omit rows after k?

For i>=k, every A[i]+B[j] is at least A[k-1]+B[0].

The k seeded pairs (0,0) through (k-1,0) are no larger.

Decision: A=[1,1,1,1], B=[2], k=3. Must row 3 be visited because it ties the cutoff?

6 / 10

Find Median from Data Stream — predict before coding

Split the multiset into a lower half and an upper half. A max-heap exposes the lower half’s largest value; a min-heap exposes the upper half’s smallest value.

Decision: Two heaps have low values {1,100} and high values {2,3}. Each heap individually obeys its own ordering and the sizes match. What wrong median do their roots imply, and which independent invariant is missing?

7 / 10

IPO — predict before coding

Feasibility depends on current capital, while desirability depends on profit. Sort once by capital requirement, then use a max-heap to choose the best profit among projects that are currently affordable.

Decision: Change the economic model: starting capital is 4, each project pays its requirement as a fee and then receives a gross reward. For one allowed project, choices (fee 4,reward 5) and (fee 1,reward 4) are affordable. Does choosing the larger reward remain optimal?

8 / 10

Design a counterexample, not a larger example

A top-frequency solver counts correctly but orders its bounded heap by numeric value.

It returns a valid set for [4,4,9,9,9,2,2], k=2. Does that validate its priority? Construct a failing input.

9 / 10

Reconstruct and transfer

For each heap, specify admission, priority, eviction, size bound, and tie policy.

Check numerical comparisons without overflowing sums.

10 / 10
←/→ or click edges to navigate · ? help · N notes · F fullscreen