Data Structures: Solve 'Find the Running Median' Using Heaps

HackerRank · 161K views · 2016 · 9:53

Open on YouTube

Algorithms & data structuresEducation & learningtutorial

Why Stack Overflow recommends it

Stack Overflow users link it as the standard explanation of the two-heap running median approach when answering questions about computing medians.

Recommended by 2 people across 2 different months, 2018 to 2021.

What it is

A short HackerRank tutorial, part of its Cracking the Coding Interview series with Gayle Laakmann McDowell, that solves the 'Find the Running Median' problem. It explains the two-heap technique, a max heap for the lower half and a min heap for the upper half, to get the median as numbers stream in.

What people said

Recommends Abhijit Gaikwad · 2018-05 · Stack Overflow · on “How to calculate the median of an array?”

You can find good explanation at [this video] The idea it to use 2 Heaps viz one max heap and mean heap. class Heap { private Queue<Integer> low = new PriorityQueue<>(Comparator.reverseOrder()); private Queue<Integer> high = new PriorityQueue<>(); public void add(int number) { Queue<Integer> target = low.size() <= high.size() ? low : high; target.add(number); balance(); } private void balance() { while(!low.isEmpty() && !high.isEmpty() && low.peek() > high.peek()) { Integer lowHead= low.poll(); Integer highHead = high.poll(); low.add(highHead); high.add(lowHead); } } public double median() { if(low.isEmpty() && high.isEmpty()) { throw new IllegalStateException("Heap is empty"); } else { return lo

Recommends Stella · 2020-04 · Stack Overflow · on “What affects the speed of the following Java code? (Fraudulent Activity Notifications on HackerRank)”

I recommend looking carefully at the definition of time complexity [LINK] I made it quite short with just one for-loop. Although it is one for loop, if the array has n elements (aka n days), and we need to calculate the days of fraud notifications after the first d days, we need to enter the for loop n-d+1 times. And each time we enter the for loop, we first copying the subarray of length d and then perform a sort on it copying: O(d) in time complexity sorting (assuming quick sort on the average case): O(d * log(d)) in time complexity. So the overall time complexity for this algorithm is O(n * d * log(d)) , if we neglect the smaller terms. The hints on using queue are for us to look at the p

Cites Ian Fitchett · 2018-02 · Stack Overflow · on “Finding Median in Order-Statistics tree in O(1) time”

I believe what Enzo Nakamura is referring to when he says finding the kth position is O(log n) is the use of a red-black binary search tree [LINK] with a certain twist [LINK] . At each node you additionally store the size of that node's subtree. This way you can take advantage of the properties of a binary search tree to find which value is the kth value in O(log n). However, the root node of a binary search tree is only guaranteed to be the median if it is a) an odd number of nodes and b) a perfectly balanced tree, and red-black binary search trees are not necessarily perfectly balanced, so even the approach above does not guarantee this. There is a sort of famous question called the " runn

Cites Abhinav Kumar · 2021-08 · Stack Overflow · on “Running Median in O(log n) time in Python”

I'm trying to find the running median of a set of numbers after each operation, where an operation can be one of the following: add x - add the number x in the set of numbers remove x - remove the number x from the set of numbers (assume x always exists in the set) My goal is to perform every operation in O(log n) time. How can this be achieved in Python? Are there any built-in constructs in Python to achieve this? If only add x operations are allowed, a possible algorithm is to maintain a minHeap (to store lower half of numbers) and a maxHeap (to store upper half of numbers), as explained in this video [this video] . But if remove x operations are allowed too, then deletion of a value will take