From e638b4218d65e50910ed43012219fc078c47668b Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Mon, 2 Nov 2026 11:30:03 +0530 Subject: [PATCH 1/2] fix(searches): add null safety and empty matrix handling to RowColumnWiseSorted2dArrayBinarySearch --- ...owColumnWiseSorted2dArrayBinarySearch.java | 11 +++- ...lumnWiseSorted2dArrayBinarySearchTest.java | 56 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java index ecdcd36adf21..940d92d9c382 100644 --- a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java @@ -20,7 +20,7 @@ * doesn't exist in the array, an array * {-1, -1} will be returned. */ -public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm { +public final class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm { @Override public > int[] find(T[][] matrix, T key) { @@ -28,10 +28,19 @@ public > int[] find(T[][] matrix, T key) { } public static > int[] search(T[][] matrix, T target) { + if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0 || target == null) { + return new int[] {-1, -1}; + } + int rowPointer = 0; // The pointer at 0th row int colPointer = matrix[0].length - 1; // The pointer at end column while (rowPointer < matrix.length && colPointer >= 0) { + if (matrix[rowPointer] == null || colPointer >= matrix[rowPointer].length) { + rowPointer++; + continue; + } + int comp = target.compareTo(matrix[rowPointer][colPointer]); if (comp == 0) { diff --git a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java index 8d1423cbeeb0..9259f57941a7 100644 --- a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java @@ -237,4 +237,60 @@ public void rowColumnSorted2dArrayBinarySearchTestTallMatrixNotFound() { assertEquals(expected[0], ans[0]); assertEquals(expected[1], ans[1]); } + + @Test + public void testNullMatrixReturnsNotFound() { + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(null, 5); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testEmptyMatrixReturnsNotFound() { + Integer[][] matrix = {}; + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testEmptyFirstRowReturnsNotFound() { + Integer[][] matrix = {{}}; + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testNullTargetReturnsNotFound() { + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, null); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testMatrixWithNullRow() { + Integer[][] matrix = { + {10, 20, 30}, + null, + {18, 28, 38}, + }; + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 28); + int[] expected = {2, 1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testFindMethodThroughInterface() { + RowColumnWiseSorted2dArrayBinarySearch searcher = new RowColumnWiseSorted2dArrayBinarySearch(); + int[] ans = searcher.find(WIDE_RECTANGULAR_MATRIX, 35); + int[] expected = {1, 2}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } } From a3fa5fc61562e0f7a8f76fa96dbb5bd97b8af687 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Fri, 6 Nov 2026 01:56:52 +0530 Subject: [PATCH 2/2] refactor(searches): scope validation to rectangular matrices per review --- .../RowColumnWiseSorted2dArrayBinarySearch.java | 5 ----- .../RowColumnWiseSorted2dArrayBinarySearchTest.java | 12 ++++-------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java index 940d92d9c382..4b66cf2a220e 100644 --- a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java @@ -36,11 +36,6 @@ public static > int[] search(T[][] matrix, T target) { int colPointer = matrix[0].length - 1; // The pointer at end column while (rowPointer < matrix.length && colPointer >= 0) { - if (matrix[rowPointer] == null || colPointer >= matrix[rowPointer].length) { - rowPointer++; - continue; - } - int comp = target.compareTo(matrix[rowPointer][colPointer]); if (comp == 0) { diff --git a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java index 9259f57941a7..6740baeb3c89 100644 --- a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java @@ -273,14 +273,10 @@ public void testNullTargetReturnsNotFound() { } @Test - public void testMatrixWithNullRow() { - Integer[][] matrix = { - {10, 20, 30}, - null, - {18, 28, 38}, - }; - int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 28); - int[] expected = {2, 1}; + public void testNullFirstRowReturnsNotFound() { + Integer[][] matrix = {null}; + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5); + int[] expected = {-1, -1}; assertEquals(expected[0], ans[0]); assertEquals(expected[1], ans[1]); }