Skip to content

Prevent overflow in sliding average updates - #100

Closed
krotname wants to merge 1 commit into
mainfrom
codex/fix-integer-overflow-in-sliding-average
Closed

Prevent overflow in sliding average updates#100
krotname wants to merge 1 commit into
mainfrom
codex/fix-integer-overflow-in-sliding-average

Conversation

@krotname

@krotname krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Motivation

  • The rolling-window update used arr.get(i) - arr.get(i - windowSize) which performs 32-bit subtraction before adding to the long accumulator and can produce incorrect averages for edge-case int inputs.

Description

  • Widen the incoming value before subtraction by changing the update to sum += (long) arr.get(i) - arr.get(i - windowSize); in SlidingAverage.movingAverage to ensure 64-bit arithmetic for the delta.
  • Add a regression unit test extremeValuesDoNotOverflowTheRollingUpdate that verifies a transition from Integer.MIN_VALUE to Integer.MAX_VALUE with windowSize = 1 produces the correct values.
  • Changes touch src/main/java/algorithms/sprint0/SlidingAverage.java and src/test/java/algorithms/sprint0/SlidingAverageTest.java and preserve existing behavior for non-extreme inputs.

Testing

  • Ran the focused unit suite with mvn -B -Dtest=algorithms.sprint0.SlidingAverageTest test and the SlidingAverage test class passed (8 tests, 0 failures).
  • Ran the full verification gate with mvn -B verify which completed successfully (all tests passed and build checks including coverage, Checkstyle, PMD, and SpotBugs showed no blocking issues).

Codex Task

@krotname

krotname commented Aug 1, 2026

Copy link
Copy Markdown
Owner Author

Superseded by the validated combined merge in #115: #115

@krotname krotname closed this Aug 1, 2026
@krotname
krotname deleted the codex/fix-integer-overflow-in-sliding-average branch August 1, 2026 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant