Skip to content

Fix: handle null key in IterativeBinarySearch#7365

Merged
DenizAltunkapan merged 4 commits intoTheAlgorithms:masterfrom
kadambari25:fix-null-key-binary-search
Apr 6, 2026
Merged

Fix: handle null key in IterativeBinarySearch#7365
DenizAltunkapan merged 4 commits intoTheAlgorithms:masterfrom
kadambari25:fix-null-key-binary-search

Conversation

@kadambari25
Copy link
Copy Markdown
Contributor

🐛 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.50%. Comparing base (741b2d1) to head (a3f2c34).

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.51%   79.50%   -0.01%     
+ Complexity     7143     7141       -2     
============================================
  Files           796      796              
  Lines         23358    23358              
  Branches       4596     4596              
============================================
- Hits          18573    18571       -2     
  Misses         4047     4047              
- Partials        738      740       +2     

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

@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) April 6, 2026 19:37
@DenizAltunkapan DenizAltunkapan merged commit 7eea9a5 into TheAlgorithms:master Apr 6, 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