Skip to content

Fix: handle null key in IterativeBinarySearch#7365

Open
kadambari25 wants to merge 3 commits intoTheAlgorithms:masterfrom
kadambari25:fix-null-key-binary-search
Open

Fix: handle null key in IterativeBinarySearch#7365
kadambari25 wants to merge 3 commits intoTheAlgorithms:masterfrom
kadambari25:fix-null-key-binary-search

Conversation

@kadambari25
Copy link
Copy Markdown

🐛 Fix: Handle null key in IterativeBinarySearch

Description

The current implementation of IterativeBinarySearch handles null and empty arrays but does not account for a null search key.

This can lead to a NullPointerException when calling:

key.compareTo(array[mid])

Changes Made

  • Added a null check for the key parameter in the find method:
if (array == null || array.length == 0 || key == null) {
    return -1;
}

Why this is important

  • Prevents runtime crashes when a null key is passed
  • Improves robustness and defensive programming
  • Ensures consistent behavior across edge cases

Type of change

  • Bug fix
  • New feature
  • Documentation update

Checklist

  • I have read the contributing guidelines
  • This is my own work
  • Code follows project conventions

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 6, 2026

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 79.47%. Comparing base (13aaad2) to head (ca3327e).

Files with missing lines Patch % Lines
.../thealgorithms/searches/IterativeBinarySearch.java 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7365   +/-   ##
=========================================
  Coverage     79.46%   79.47%           
+ Complexity     7131     7130    -1     
=========================================
  Files           795      795           
  Lines         23297    23297           
  Branches       4587     4587           
=========================================
+ Hits          18513    18515    +2     
+ Misses         4047     4046    -1     
+ Partials        737      736    -1     

☔ View full report in Codecov by Sentry.
📢 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.

@kadambari25
Copy link
Copy Markdown
Author

Hi, I’ve added a null check for the key parameter to prevent potential NullPointerException. Please let me know if any changes are needed.

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