Skip to content

fix(searches): avoid int overflow and short-text crash in RabinKarpAlgorithm - #7665

Merged
DenizAltunkapan merged 2 commits into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/rabinkarp-overflow
Nov 4, 2026
Merged

DenizAltunkapan merged 2 commits into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/rabinkarp-overflow

Conversation

@Rishavbhattarai

@Rishavbhattarai Rishavbhattarai commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Two bugs in searches/RabinKarpAlgorithm.search:

  1. The rolling hash is an int, so ALPHABET_SIZE * hash overflows for large primes. With the common prime 1_000_000_007, I checked 5,000 random lowercase texts against a pattern taken from each one, and 1,110 returned the wrong index. Example: search("jggw", "nzdjsjggwx", 1_000_000_007) returns -1 instead of 5. The hashes are now long.
  2. When the pattern is longer than the text, the first hash loop reads past the end of the text: search("ABC", "AB", 101) throws StringIndexOutOfBoundsException. It now returns -1.

Both cases are added to the existing parameterized test and fail on master.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations. (No new algorithm.)
  • All new algorithms include a corresponding test class that validates their functionality. (Regression test added.)
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.26%. Comparing base (e6b2ecb) to head (c2304f0).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7665      +/-   ##
============================================
- Coverage     82.26%   82.26%   -0.01%     
+ Complexity     8408     8406       -2     
============================================
  Files           843      843              
  Lines         26140    26142       +2     
  Branches       5100     5101       +1     
============================================
  Hits          21505    21505              
  Misses         3843     3843              
- Partials        792      794       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rishavbhattarai
Rishavbhattarai marked this pull request as ready for review October 27, 2026 03:09

@DenizAltunkapan DenizAltunkapan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean implementation, thank you. @Rishavbhattarai

@DenizAltunkapan
DenizAltunkapan enabled auto-merge (squash) November 4, 2026 11:03
@DenizAltunkapan
DenizAltunkapan merged commit 4f535f5 into TheAlgorithms:master Nov 4, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants