Challenge - 5 Problems
2D Matrix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of search in sorted 2D matrix
What is the output of the following Java code that searches for the number 5 in a sorted 2D matrix?
DSA Java
public class SearchMatrix { public static boolean searchMatrix(int[][] matrix, int target) { int rows = matrix.length; int cols = matrix[0].length; int r = 0, c = cols - 1; while (r < rows && c >= 0) { if (matrix[r][c] == target) { return true; } else if (matrix[r][c] > target) { c--; } else { r++; } } return false; } public static void main(String[] args) { int[][] matrix = { {1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50} }; System.out.println(searchMatrix(matrix, 5)); } }
Attempts:
2 left
💡 Hint
The search starts from top-right corner and moves left or down based on comparison.
✗ Incorrect
The code correctly implements a search in a sorted 2D matrix by starting at the top-right corner and moving left if the current element is greater than target or down if smaller. Since 5 is present, it returns true.
❓ Predict Output
intermediateOutput when target is not in matrix
What will be the output of this code when searching for 13 in the same matrix?
DSA Java
public class SearchMatrix { public static boolean searchMatrix(int[][] matrix, int target) { int rows = matrix.length; int cols = matrix[0].length; int r = 0, c = cols - 1; while (r < rows && c >= 0) { if (matrix[r][c] == target) { return true; } else if (matrix[r][c] > target) { c--; } else { r++; } } return false; } public static void main(String[] args) { int[][] matrix = { {1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50} }; System.out.println(searchMatrix(matrix, 13)); } }
Attempts:
2 left
💡 Hint
Check if 13 exists in the matrix by following the search path.
✗ Incorrect
The search moves through the matrix but does not find 13, so it returns false.
🔧 Debug
advancedIdentify the error in matrix search code
What error does this code produce when run, and why?
DSA Java
public class SearchMatrix { public static boolean searchMatrix(int[][] matrix, int target) { int rows = matrix.length; int cols = matrix[0].length; int r = 0, c = cols - 1; while (r <= rows && c >= 0) { if (matrix[r][c] == target) { return true; } else if (matrix[r][c] > target) { c--; } else { r++; } } return false; } public static void main(String[] args) { int[][] matrix = { {1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50} }; System.out.println(searchMatrix(matrix, 100)); } }
Attempts:
2 left
💡 Hint
Check the loop condition for row index boundary.
✗ Incorrect
The loop condition uses r <= rows which allows r to equal rows, causing an index out of bounds when accessing matrix[r][c].
🧠 Conceptual
advancedTime complexity of search in sorted 2D matrix
What is the time complexity of searching for a target in an m x n matrix where each row and column is sorted in ascending order using the top-right corner search method?
Attempts:
2 left
💡 Hint
Each step moves either one row down or one column left.
✗ Incorrect
At most, the search moves m times down and n times left, so total steps are O(m + n).
🚀 Application
expertFind the number of elements less than or equal to target
Given a sorted 2D matrix (each row and column sorted ascending), which code snippet correctly counts how many elements are less than or equal to a target value?
Attempts:
2 left
💡 Hint
Use the property of sorted rows and columns to count efficiently from top-right corner.
✗ Incorrect
Starting from top-right, if matrix[r][c] <= target, all elements in that row before c are <= target, so add c+1 and move down. Else move left.
