Skip to content

fix(searches): add null safety and empty matrix handling to RowColumnWiseSorted2dArrayBinarySearch - #7725

Open
hardikkaurani wants to merge 2 commits into
TheAlgorithms:masterfrom
hardikkaurani:fix/row-col-sorted-2d-array-null-safety
Open

hardikkaurani wants to merge 2 commits into
TheAlgorithms:masterfrom
hardikkaurani:fix/row-col-sorted-2d-array-null-safety

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor
  • 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

Description

This pull request adds null safety, empty matrix handling, and marks RowColumnWiseSorted2dArrayBinarySearch final:

  1. Null and Empty Input Validation: Added guards against matrix == null, empty 2D array (matrix.length == 0), empty first row (matrix[0].length == 0), and target == null, returning {-1, -1} safely.
  2. Jagged/Null Row Safety: Inside the pointer iteration loop, verified that each row is non-null and that colPointer < matrix[rowPointer].length before reading elements to prevent NullPointerException and ArrayIndexOutOfBoundsException.
  3. Class Modifier: Marked public final class RowColumnWiseSorted2dArrayBinarySearch.
  4. Expanded Tests: Added unit tests covering null matrix, empty 2D array, matrix with empty first row, null target, matrix containing a null row, and verifying the find() interface method.

Copilot AI balanced review requested due to automatic review settings November 2, 2026 06:04

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 insufficient remaining quota. Try requesting a review at a lower effort level.

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

hardikkaurani commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @DenizAltunkapan,

Could you please review this pull request when you have a moment?

Summary of Changes

  • Null and Empty Input Handling: Added null and empty boundary checks in RowColumnWiseSorted2dArrayBinarySearch.search() to safely return {-1, -1} for null or empty inputs.
  • Ragged Array Safety: Added guards against null or shorter rows during pointer traversal in search() to prevent invalid index access.
  • Class Visibility: Declared RowColumnWiseSorted2dArrayBinarySearch as public final.
  • Test Coverage: Added unit tests covering null inputs, empty matrices, jagged arrays, and interface validation.

Test Results

  • RowColumnWiseSorted2dArrayBinarySearchTest: 25 passed, 0 failed, 0 skipped (BUILD SUCCESS).
  • Formatted the changes using clang-format in accordance with project conventions.

Thank you for your time and consideration!

@codecov-commenter

codecov-commenter commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.28%. Comparing base (8b49bc5) to head (a3fa5fc).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7725      +/-   ##
============================================
+ Coverage     82.23%   82.28%   +0.05%     
- Complexity     8402     8418      +16     
============================================
  Files           843      843              
  Lines         26140    26148       +8     
  Branches       5100     5103       +3     
============================================
+ Hits          21495    21516      +21     
+ Misses         3853     3840      -13     
  Partials        792      792              

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

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.

🟡 Changes recommended

The search skips valid elements in shorter rows, causing existing targets to be reported as absent; it needs corrected handling and a regression test.

1 open issue

Review effort: Balanced

Comment on lines +39 to +41
if (matrix[rowPointer] == null || colPointer >= matrix[rowPointer].length) {
rowPointer++;
continue;

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.

@hardikkaurani please check this

The null and empty input handling is a useful improvement. However, we found a correctness issue with the newly added support for jagged arrays.

Consider the following example:

Integer[][] matrix = {
    {1, 4, 7},
    {5}
};

Searching for 5 returns {-1, -1} instead of {1, 0} because the new bounds check skips the second row entirely when colPointer exceeds its length.

The current staircase search algorithm assumes a rectangular matrix with rows and columns sorted in ascending order. Supporting jagged arrays requires additional consideration to preserve correctness.

I would suggest keeping this PR focused on null and empty input validation for rectangular matrices, rather than introducing partial support for jagged arrays.

Please also add a regression test for the case above if jagged arrays are intended to be supported.

Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed just making some changes !

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated as suggested! Removed the partial jagged array checks to keep the implementation strictly focused on null and empty input validation for rectangular matrices, and replaced the jagged test with a null-first-row boundary test ({null}).

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi @DenizAltunkapan,

I have updated the PR following your feedback:

  • Removed the partial jagged array checks from the search loop so the algorithm stays strictly focused on rectangular row- and column-wise sorted matrices.
  • Retained the null and empty input guards (matrix == null, matrix.length == 0, matrix[0] == null, matrix[0].length == 0, and arget == null).
  • Replaced the null-row test case with a boundary test for a null-first-row matrix ({null}).
  • Ran tests and formatted files with clang-format. All 25 tests pass cleanly.

Please let me know if everything looks good or if any further adjustments are needed. Thank you!

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