interviewstreet median challenge
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Overview
The InterviewStreet Median Challenge is a programming problem that tests a candidate's ability to calculate the median of a dynamic set of numbers efficiently. This challenge is a common feature in technical interviews, as it assesses both algorithmic thinking and data structure management.
Problem Description
The challenge involves processing a sequence of integers. For each number added to the sequence, you are required to output the median of the current sequence.
Given the nature of the problem, it requires not only understanding the definition of median but also devising an efficient algorithm to compute it as numbers are added one by one.
Definition of Median
The median is a fundamental statistical measure that represents the middle value of a data set:
- For a set with an odd number of elements, the median is the middle element when sorted.
- For a set with an even number of elements, the median is the average of the two middle elements when sorted.
For example:
- In the list
[1, 3, 3, 6, 7, 8, 9], the median is6. - In the list
[1, 2, 3, 4, 5, 6, 8, 9], the middle elements are4and5, so the median is(4 + 5) / 2 = 4.5.
Technical Explanation
Naive Approach
A naive solution to this problem would involve inserting each number into a list and then sorting the list to find the median after each insertion. While this approach is straightforward, it is inefficient. The sorting step has a time complexity of , which makes this approach infeasible for a large number of updates.
Efficient Approach Using Heaps
A more efficient approach involves using two heaps (priority queues):
- Max-Heap: To store the first half of the numbers, allowing access to the maximum of the lower half.
- Min-Heap: To store the second half of the numbers, allowing access to the minimum of the upper half.
Algorithm
- When a new number
xis added:- Compare
xto the max element of the Max-Heap (let's call itmax_l):- If
x <= max_l, insertxinto the Max-Heap. - Else, insert
xinto the Min-Heap.
- Balance the heaps:
- If the size of the Max-Heap is greater than one more than the size of the Min-Heap, move the root of the Max-Heap to the Min-Heap.
- If the size of the Min-Heap is greater than the size of the Max-Heap, move the root of the Min-Heap to the Max-Heap.
- The median is:
- The root of the Max-Heap if it has more elements.
- The root of the Min-Heap if both heaps are of equal size, the median is the average of both roots.
Example
Consider the sequence: [6, 10, 2, 4, 3, 8, 7]
| Step | Add | Max-Heap | Min-Heap | Median |
| 1 | 6 | |||
[6] | ||||
[] | ||||
6 | ||||
| 2 | 10 | |||
[6] | ||||
[10] | ||||
8 | ||||
| 3 | 2 | |||
[2, 6] | ||||
[10] | ||||
6 | ||||
| 4 | 4 | |||
[4, 2] | ||||
[6, 10] | ||||
5 | ||||
| 5 | 3 | |||
[3, 2] | ||||
[4, 6, 10] | ||||
4 | ||||
| 6 | 8 | |||
[4, 3, 2] | ||||
[6, 8, 10] | ||||
5 | ||||
| 7 | 7 | |||
[6, 4, 3, 2] | ||||
[7, 8, 10] | ||||
6.5 | ||||
Complexity Analysis
Using heaps results in operations that can be executed in logarithmic time with respect to the number of elements, leading to the following complexities:
- Time Complexity: for each insertion and balancing operation.
- Space Complexity: for storing the elements in the two heaps.
Conclusion
The InterviewStreet Median Challenge is an excellent test of one's ability to handle dynamic data using sophisticated data structures. By leveraging Max and Min Heaps, one can efficiently compute the median in a stream of numbers with an optimal time complexity, thereby making this approach highly scalable and suitable for real-world applications where latency and performance are critical factors.

