Skip to content

fix(searches): support duplicates and null safety in LowerBound and UpperBound - #7648

Merged
alxkm merged 4 commits into
TheAlgorithms:masterfrom
hardikkaurani:fix/lower-bound-duplicate-support-and-null-safety
Oct 21, 2026
Merged

alxkm merged 4 commits into
TheAlgorithms:masterfrom
hardikkaurani:fix/lower-bound-duplicate-support-and-null-safety

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

This pull request addresses two issues in LowerBound and UpperBound:

  1. Duplicate Elements in LowerBound:
    The Javadoc for LowerBound specifies: "If there are multiple values that are equal to val it returns the index of the first such value."
    Previously, when comp == 0, search() immediately returned median. When an array contained duplicates (e.g., [1, 2, 2, 2, 3]), this returned an intermediate median index instead of the first occurrence. Changing the condition to if (comp <= 0) ensures the search continues exploring the left half to find the first matching element index.
  2. Null and Empty Array Safety:
    Added edge case checks in LowerBound.find() and UpperBound.find() to return 0 when array == null, array.length == 0, or key == null, preventing NullPointerException and unexpected behavior on null/empty inputs.
  3. Unit Tests:
    Added tests in LowerBoundTest covering duplicate elements, all-identical arrays, empty arrays, null arrays, and null keys, along with null checks in UpperBoundTest.
  • 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.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

When multiple elements equal to key exist, LowerBound should return the index of the first such value per its specification. Continue searching the left partition when comp <= 0 instead of prematurely returning the median element.
…rBound

Guard against NullPointerException and empty array bounds access in LowerBound and UpperBound find methods when array is null/empty or key is null.
…ll safety

Verify LowerBound returns the first occurrence for duplicate elements, and handles empty arrays, null arrays, and null keys gracefully in LowerBound and UpperBound.
Copilot AI balanced review requested due to automatic review settings October 21, 2026 11:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi @alxkm ! 👋

This PR is ready for review:

  1. Duplicate Elements in LowerBound: Per the LowerBound specification ("If there are multiple values that are equal to val it returns the index of the first such value"), updated the condition to search left when comp <= 0 instead of prematurely returning the median element.
  2. Null & Empty Array Defensiveness: Safely handles null array, empty array (length == 0), and null key in both LowerBound and UpperBound.
  3. Tests & Commits: Structured into 3 atomic commits with comprehensive JUnit test coverage for all-same arrays, duplicate sequences, and null/empty boundary inputs.

Thank you!

@codecov-commenter

codecov-commenter commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.19%. Comparing base (d9b86d1) to head (4d50b7f).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7648      +/-   ##
============================================
+ Coverage     82.17%   82.19%   +0.01%     
- Complexity     8367     8375       +8     
============================================
  Files           842      842              
  Lines         26093    26095       +2     
  Branches       5090     5089       -1     
============================================
+ Hits          21443    21448       +5     
  Misses         3858     3858              
+ Partials        792      789       -3     

☔ 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.

@alxkm alxkm 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.

Thanks! The duplicate fix in LowerBound looks correct, and the tests cover it well.

Three small things:

  1. Returning 0 for a null array/key silently hides caller errors. Would Objects.requireNonNull(...) (with assertThrows in tests) be better? If we keep 0, please document it in @return.
  2. Without the early return, LowerBound's best case is no longer O(1). Please update the Javadoc.
  3. Could you add a duplicates test for UpperBound too (e.g. {1, 2, 2, 2, 3}, key 2 → 4)?

@hardikkaurani
hardikkaurani force-pushed the fix/lower-bound-duplicate-support-and-null-safety branch from 81c661e to 4d50b7f Compare October 21, 2026 15:32
@hardikkaurani

hardikkaurani commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @alxkm,

Thank you for the review feedback! I have updated the PR to address all three points.

@hardikkaurani
hardikkaurani requested a review from alxkm October 21, 2026 15:37

@alxkm alxkm 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.

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit f8207cc into TheAlgorithms:master Oct 21, 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.

4 participants