TheAlgorithms/Java · warning · IllegalArgumentException
Median is undefined for an empty data set.
Error message
Median is undefined for an empty data set.
What it means
Thrown by MedianOfRunningArray.getMedian when both internal heaps are empty, i.e., insert() has never been called. The class maintains a two-heap median structure (max-heap for the lower half, min-heap for the upper half); with no elements there is no defined median. It is an abstract generic class parameterized by a Number type.
Source
Thrown at src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java:47
public final void insert(final T element) {
if (!minHeap.isEmpty() && element.compareTo(minHeap.peek()) < 0) {
maxHeap.offer(element);
balanceHeapsIfNeeded();
} else {
minHeap.offer(element);
balanceHeapsIfNeeded();
}
}
/**
* Returns the median of the current elements.
*
* @return the median value
* @throws IllegalArgumentException if no elements have been inserted
*/
public final T getMedian() {
if (maxHeap.isEmpty() && minHeap.isEmpty()) {
throw new IllegalArgumentException("Median is undefined for an empty data set.");
}
if (maxHeap.size() == minHeap.size()) {
return calculateAverage(maxHeap.peek(), minHeap.peek());
}
return (maxHeap.size() > minHeap.size()) ? maxHeap.peek() : minHeap.peek();
}
/**
* Calculates the average between two values.
* Concrete subclasses must define how averaging works (e.g., for Integer, Double, etc.).
*
* @param a first number
* @param b second number
* @return the average of a and b
*/
protected abstract T calculateAverage(T a, T b);View on GitHub (pinned to fdfb9a395b)
Solutions
- Track an insertion count (or check a flag) and skip/return a sentinel until at least one element is inserted.
- Ensure at least one insert() precedes the first getMedian() call in your flow.
- If an empty median is meaningful in your domain, branch before getMedian and return null/Optional.empty.
Example fix
// before
MedianOfRunningArray<Integer> med = new MedianOfRunningArrayInteger();
Integer m = med.getMedian(); // throws
// after
MedianOfRunningArray<Integer> med = new MedianOfRunningArrayInteger();
Optional<Integer> m = med.isEmpty()
? Optional.empty()
: Optional.of(med.getMedian()); Defensive patterns
Strategy: validation
Validate before calling
// MedianOfRunningArray has no isEmpty() in base; track insertions yourself boolean hasData = insertedCount > 0; T median = hasData ? tracker.getMedian() : null;
Try / catch
try {
T m = tracker.getMedian();
} catch (IllegalArgumentException e) {
// no data inserted yet; treat as no median available
return Optional.empty();
} Prevention
- Maintain a counter of insert() calls and guard getMedian with it.
- In streaming jobs, only compute the median after the first window has data.
When it happens
Trigger: Constructing a MedianOfRunningArray subclass and calling getMedian() before any insert(element). Also when all inserted elements were conceptually cleared (though no clear method exists in the base class).
Common situations: Calling getMedian on a freshly created tracker before the stream starts, or a streaming job where the first window had no events yet.
Related errors
- Cannot insert null into the heap.
- Cannot extract from empty heap
- Cannot insert null element
- Cannot delete from empty heap
- Input arrays are not sorted
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/24f3cb129905ee0e.
Report an issue: GitHub.