Kth Largest in a Stream (Online) - Watch the Algorithm Execute, Step by Step
Watching each heap operation and decision helps you understand how the algorithm efficiently keeps track of the kth largest element without sorting the entire stream.
Step 1/13
·Active fill★Answer cell
heapifymin-heap
2
0
4
1
8
2
5
3
extract_minmin-heap
4
0
5
1
8
2
peekmin-heap
4
0
5
1
8
2
Added: 4
heapify_downmin-heap
5
0
5
1
8
2
Added: 5
heapify_downmin-heapswap [0↔2]
5
0
10
1
8
2
Added: 5
heapify_downmin-heapswap [0↔2]
8
0
10
1
9
2
Added: 8
peekmin-heap
8
0
10
1
9
2
Added: 8
peekmin-heap
4
0
5
1
8
2
Added: 4
peekmin-heap
4
0
5
1
8
2
Added: 4
peekmin-heap
5
0
5
1
8
2
Added: 5
peekmin-heap
5
0
10
1
8
2
Added: 5
peekmin-heap
8
0
10
1
9
2
Added: 8
peekmin-heap
8
0
10
1
9
2
Added: 8
Key Takeaways
✓ Maintaining a min-heap of size k efficiently tracks the kth largest element in a stream.
This insight is hard to see from code alone because the heap operations and their effect on the kth largest element are abstract without visualization.
✓ Only values larger than the current kth largest (heap root) affect the heap, minimizing unnecessary operations.
Visualizing each comparison and decision clarifies why some adds are ignored and others cause heap updates.
✓ The root of the min-heap always holds the kth largest element, even as the heap contents change dynamically.
Seeing the root update step-by-step helps understand the heap invariant and its role in the algorithm.
Practice
(1/5)
1. Consider the following Python code implementing the heap-based approach to find the k-th smallest pair distance. What is the returned value when calling smallestDistancePair([1,3,1], 2)?
Pop (0,0,1). Since j+1=2<n, push (3-1=2,0,2). Heap now: (2,0,2), (2,1,2). The smallest distance at heap top is 2.
Step 3: Return the k-th smallest distance
After popping once, the top of the heap is (2,0,2), so the returned value is 2.
Final Answer:
Option D -> Option D
Quick Check:
Second smallest pair distance is 2 [OK]
Hint: Trace heap pops and pushes carefully for small inputs [OK]
Common Mistakes:
Off-by-one in k-1 loop
Confusing indices i,j
Returning wrong heap element
2. Consider the following code snippet for finding the kth largest XOR coordinate value. Given the input matrix = [[5, 2], [1, 6]] and k = 3, what is the value of min_heap[0] returned by the function?
Values inserted: 5, 7, 4. The heap after these is [4,7,5]. The next value 0 is less than min_heap[0]=4, so ignored. The kth largest is min_heap[0] = 4.
Final Answer:
Option A -> Option A
Quick Check:
Heap after all insertions has 4 as smallest among top 3 [OK]
Hint: Trace prefix XOR and heap insertions carefully [OK]
Common Mistakes:
Off-by-one in prefix indices
Miscomputing XOR values
Confusing heap root with max value
3. Consider the following Python code implementing the optimal approach to find the kth smallest element in a sorted matrix. Given the matrix [[1,5],[6,10]] and k = 3, what is the final returned value?
Now low=6, high=10; mid=8; countLessEqual(8) counts elements ≤8: row=1,col=0; matrix[1][0]=6≤8 -> count=2, col=1; matrix[1][1]=10>8 -> row=0; matrix[0][1]=5≤8 -> count=3, col=2 (end). Total=3 ≥ k=3, so high=mid=8.
Step 3: Continue binary search until low=high=6
Eventually low converges to 6, which is the 3rd smallest element.
Final Answer:
Option A -> Option A
Quick Check:
Binary search narrows to 6 as 3rd smallest [OK]
Hint: Count elements ≤ mid guides binary search [OK]
Common Mistakes:
Off-by-one in k indexing
Miscounting elements ≤ mid
4. Suppose the problem changes: now characters can be reused unlimited times (like infinite supply), and you want to return a string of length k with characters sorted by frequency descending. Which modification to the bucket sort approach correctly handles this variant?
hard
A. Use a max heap to repeatedly extract the highest frequency character and append it until length k is reached, decrementing frequency each time.
B. Use the same bucket sort but truncate the final concatenated string to length k.
C. Use bucket sort but repeat characters infinitely in buckets until length k is reached.
D. Sort characters by frequency once, then repeat the entire sorted string until length k is reached.
Solution
Step 1: Understand infinite reuse constraint
Characters can be used multiple times, frequencies decrease as used.
Step 2: Why max heap is needed
Max heap allows extracting highest frequency char, appending it, then updating frequency dynamically.
5. Suppose you want to find the top k frequent words but now words can be reused infinitely many times (like unlimited supply). Which modification to the optimal bucket sort approach correctly handles this scenario?
hard
A. This problem reduces to a knapsack variant; bucket sort no longer applies
B. Modify frequency counting to consider infinite reuse, then apply bucket sort as usual
C. Use a dynamic programming approach to count combinations of words for frequencies
D. Use a max heap keyed by frequency and word lex order, repeatedly pop top k elements
Solution
Step 1: Understand infinite reuse impact
Infinite reuse means frequency counts are not fixed; problem becomes combinatorial.
Step 2: Recognize problem class change
Top k frequent words with unlimited reuse resembles knapsack or coin change, not simple frequency counting.
Step 3: Bucket sort no longer applies
Bucket sort relies on fixed frequencies; dynamic programming or other combinatorial methods are needed.
Final Answer:
Option A -> Option A
Quick Check:
Infinite reuse breaks frequency counting assumptions [OK]
Hint: Infinite reuse changes problem class, bucket sort invalid [OK]
Common Mistakes:
Trying to reuse bucket sort with infinite counts
Ignoring combinatorial explosion
Assuming heap approach suffices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.