Skip to content

Commit 07c7ea3

Browse files
fix(searches): add null safety and empty matrix handling to RowColumnWiseSorted2dArrayBinarySearch (#7725)
* fix(searches): add null safety and empty matrix handling to RowColumnWiseSorted2dArrayBinarySearch * refactor(searches): scope validation to rectangular matrices per review
1 parent 3c4f19d commit 07c7ea3

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
* doesn't exist in the array, an array
2121
* {-1, -1} will be returned.
2222
*/
23-
public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm {
23+
public final class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm {
2424

2525
@Override
2626
public <T extends Comparable<T>> int[] find(T[][] matrix, T key) {
2727
return search(matrix, key);
2828
}
2929

3030
public static <T extends Comparable<T>> int[] search(T[][] matrix, T target) {
31+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0 || target == null) {
32+
return new int[] {-1, -1};
33+
}
34+
3135
int rowPointer = 0; // The pointer at 0th row
3236
int colPointer = matrix[0].length - 1; // The pointer at end column
3337

‎src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,56 @@ public void rowColumnSorted2dArrayBinarySearchTestTallMatrixNotFound() {
237237
assertEquals(expected[0], ans[0]);
238238
assertEquals(expected[1], ans[1]);
239239
}
240+
241+
@Test
242+
public void testNullMatrixReturnsNotFound() {
243+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(null, 5);
244+
int[] expected = {-1, -1};
245+
assertEquals(expected[0], ans[0]);
246+
assertEquals(expected[1], ans[1]);
247+
}
248+
249+
@Test
250+
public void testEmptyMatrixReturnsNotFound() {
251+
Integer[][] matrix = {};
252+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5);
253+
int[] expected = {-1, -1};
254+
assertEquals(expected[0], ans[0]);
255+
assertEquals(expected[1], ans[1]);
256+
}
257+
258+
@Test
259+
public void testEmptyFirstRowReturnsNotFound() {
260+
Integer[][] matrix = {{}};
261+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5);
262+
int[] expected = {-1, -1};
263+
assertEquals(expected[0], ans[0]);
264+
assertEquals(expected[1], ans[1]);
265+
}
266+
267+
@Test
268+
public void testNullTargetReturnsNotFound() {
269+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, null);
270+
int[] expected = {-1, -1};
271+
assertEquals(expected[0], ans[0]);
272+
assertEquals(expected[1], ans[1]);
273+
}
274+
275+
@Test
276+
public void testNullFirstRowReturnsNotFound() {
277+
Integer[][] matrix = {null};
278+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5);
279+
int[] expected = {-1, -1};
280+
assertEquals(expected[0], ans[0]);
281+
assertEquals(expected[1], ans[1]);
282+
}
283+
284+
@Test
285+
public void testFindMethodThroughInterface() {
286+
RowColumnWiseSorted2dArrayBinarySearch searcher = new RowColumnWiseSorted2dArrayBinarySearch();
287+
int[] ans = searcher.find(WIDE_RECTANGULAR_MATRIX, 35);
288+
int[] expected = {1, 2};
289+
assertEquals(expected[0], ans[0]);
290+
assertEquals(expected[1], ans[1]);
291+
}
240292
}

0 commit comments

Comments
 (0)