Repository navigation
fix(misc): prevent overflow when averaging the two middle values of a running median - #7781
Merged
Merged
Conversation
shreyanshj10
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 24, 2026 10:49
MedianOfRunningArrayInteger and MedianOfRunningArrayLong both compute (a + b) / 2, which overflows once the two middle values of the stream are large. For an Integer stream holding Integer.MAX_VALUE and Integer.MAX_VALUE - 2 the median came back as -2 instead of 2147483646; the Long version fails the same way near Long.MAX_VALUE. Sum in a long for the Integer case, and in a BigInteger for the Long case since there is no wider primitive. Add regression tests for both types at each end of their range.
shreyanshj10
force-pushed
the
fix-median-overflow
branch
from
November 24, 2026 11:59
1412e33 to
325f145
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7781 +/- ##
=========================================
Coverage 82.45% 82.45%
+ Complexity 8515 8514 -1
=========================================
Files 847 847
Lines 26292 26293 +1
Branches 5134 5134
=========================================
+ Hits 21680 21681 +1
Misses 3824 3824
Partials 788 788 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
alxkm
approved these changes
Nov 24, 2026
alxkm
left a comment
Member
There was a problem hiding this comment.
Looks good. Thank you for the contribution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MedianOfRunningArrayreturnscalculateAverage(maxHeap.peek(), minHeap.peek())whenever thestream holds an even number of values, and two of the six subclasses compute that average as
(a + b) / 2. The sum overflows once the two middle values are large, so the median comes backwrong with no error:
MedianOfRunningArrayInteger: insertInteger.MAX_VALUEand thenInteger.MAX_VALUE - 2, andgetMedian()returns-2instead of2147483646. NearInteger.MIN_VALUEit returns1instead of
-2147483647.MedianOfRunningArrayLongfails the same way nearLong.MAX_VALUEandLong.MIN_VALUE.For the
Integersubclass I sum into alongbefore halving. For theLongsubclass there is nowider primitive, so I sum into a
BigInteger;BigInteger.dividetruncates toward zero, which iswhat the existing behaviour does, so negative medians are unchanged (
testWithNegativeValuesstill asserts that the median of
-1and-2is-1). I checked that against the old expressionover every
intandlongpair in[-200, 200], two million randomlongpairs and the values atboth ends of each range: the new code agrees everywhere the old code did not overflow, and matches
the exact midpoint everywhere else.
I left the
Bytesubclass alone because it cannot overflow: its operands promote tointand theaverage of two bytes is always back in range. The
FloatandDoublesubclasses do have a relatedproblem -- the sum reaches
Infinityfor values nearMAX_VALUE-- but the obviousa / 2 + b / 2rewrite returns
0.0for two subnormals, so that one needs more thought than this fix and I wouldrather keep it separate.
The four new tests in
MedianOfRunningArrayTestfail on the current code with the values above andpass with this change. The other 17 tests in that class are untouched and still pass.
MedianOfRunningArrayTestclang-format -i --style=file path/to/your/file.java