Kth Largest Element in a Stream — Min-Heap Design [Amazon Easy]
Design a class to find the kth largest element in a data stream. Min-heap of size k: the top is always the answer. Full C++, Java, JavaScript and Python.
webcoderspeed.com
47 articles
Design a class to find the kth largest element in a data stream. Min-heap of size k: the top is always the answer. Full C++, Java, JavaScript and Python.
Master heaps and priority queues: 7 core patterns including Top K, Two Heaps, Merge K, and scheduling problems.
Maintain a min-heap of size K to track the Kth largest element as numbers are added to a stream.
Simulate stone smashing by repeatedly extracting the two heaviest stones using a max-heap.
Assign gold/silver/bronze or rank numbers to athletes based on their scores using sorted ordering.
Find K closest elements to x in a sorted array using binary search to find the optimal left boundary.
Find the Kth largest element using quickselect O(n) average or a min-heap of size K in O(n log k).
Return the K most frequent elements using a frequency map plus min-heap, or bucket sort for O(n) solution.
Sort characters in a string by decreasing frequency using a frequency counter and max-heap or bucket sort.
Find K points closest to the origin using a max-heap of size K or quickselect for O(n) average.
Find minimum CPU intervals to schedule tasks with cooldown using a greedy max-heap simulation.
Rearrange a string so no two adjacent characters are the same using a greedy max-heap approach.
Maintain a dynamic median using two heaps: a max-heap for the lower half and a min-heap for the upper half.
Compute medians of all sliding windows using two heaps with lazy deletion for element removal.
Merge K sorted linked lists into one sorted list using a min-heap to always extract the globally smallest node.
Find the Kth smallest element in an n×n row-and-column sorted matrix using binary search on value range or heap.
Find K pairs (u, v) with smallest sums from two sorted arrays using a min-heap initialized with first row candidates.
Find the nth ugly number (only prime factors 2, 3, 5) using three pointers for the 2x, 3x, 5x merge streams.
Maximize courses taken within deadlines by greedily scheduling and swapping out the longest past course.
Find minimum conference rooms required by tracking room end times with a min-heap of ongoing meeting endings.
Find minimum cost to connect all sticks by greedily always merging the two shortest sticks first.
Maximize capital after k IPO investments by greedily picking the highest profit project among affordable ones.
Check if a car can pick up all passengers by processing start/end events with a sorted timeline or heap.
Greedily use ladders for the largest climbs and bricks for smaller gaps, managed with a min-heap of ladder sizes.
Assign tasks to available servers using two heaps: one for free servers and one for busy servers sorted by free time.
Calculate trapped water in a 3D height map using BFS with a min-heap to process cells from shortest boundary inward.
Find minimum time to swim from top-left to bottom-right in a grid where time = max elevation on the path.
Find the smallest range that contains at least one element from each of K sorted lists using a sliding K-way merge.
Find the nth super ugly number with prime factors only from a given list using K-pointer dynamic programming.
Find minimum fuel stops to reach the target by greedily selecting the largest available fuel stations when running low.
Design a simplified Twitter with follow/unfollow and a news feed showing the 10 most recent tweets from followed users.
Find the nth number divisible by a, b, or c using binary search with inclusion-exclusion counting formula.
Maximize team performance (sum of speeds * min efficiency) by sorting by efficiency and using a min-heap on speeds.
Build the longest string with at most 2 consecutive identical characters by always using the most frequent available character.
Design a stack that pops the most frequent element, breaking ties by most recently pushed, using frequency-bucket stacks.
Minimize max-min of an array by doubling odd numbers and repeatedly halving the max using a max-heap.
Extended practice: compute median for each window in a stream using two-heap with lazy deletion pattern.
For each query point, find the smallest interval containing it by sorting both intervals and queries, using a min-heap.
For each interval find the interval with the smallest start point >= the end point using sorted starts and binary search.
Sort array elements by frequency ascending, breaking ties by value descending.
Find the kth largest level sum in a binary tree using BFS to compute level sums then sorting or using a heap.
Minimize total hiring cost by always choosing the cheapest candidate from the first or last p candidates using two min-heaps.
Design a seat manager that reserves the lowest numbered available seat and unreserves seats using a min-heap.
Track stock prices with corrections by maintaining a sorted multiset and a timestamp-to-price map.
Find the minimum number of elements to remove to reduce array size by half by greedily removing most frequent elements.
Find the subsequence of length k with the largest sum by selecting top k values while preserving original order.
Complete Heaps section recap with 7 patterns, complexity guide, and full problem index for interview prep.