Two Heaps for Median FindingHeap (Priority Queue) Patterns

The Two Heaps for Median Finding pattern applies when you need the Two Heaps for Median Finding technique within the Heap (Priority Queue) Patterns pattern. Its time complexity is O(log n) per insertion and space complexity O(n). It is used in 2 problems on Thita, including Finding MK Average and Find Median from Data Stream. Common variations are Top K Elements (Selection/Frequency) and K-way Merge.

Master the two heaps technique to find median from data stream in O(log n) time.

Two Heaps for Median Finding is one of the 4 subpatterns of the Heap (Priority Queue) Patterns pattern, which covers top K elements, K-way merge, two heaps for median, and scheduling. The whole pattern is about 2 hours of study. This subpattern is an important variation worth recognising on sight.

What Two Heaps for Median Finding covers

Master heap-based algorithms including top K elements, two heaps for median, K-way merge, and scheduling problems with priority queues. Problems in this subpattern are usually searched for as find median stream, two heaps, median data stream, min max heap, leetcode 295.

How to practise Two Heaps for Median Finding on Thita.ai

Read the theory for Two Heaps for Median Finding, then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the Heap (Priority Queue) Patterns tracker shows this one as covered once you have solved its problems.

Other subpatterns in Heap (Priority Queue) Patterns

Related DSA patterns

Where to go next

Two Heaps for Median Finding is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the Heap (Priority Queue) Patterns guide is the shortest path back to the rest of it.

Preparing this lesson and its course outline.