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 C# code that searches for the target 5 in the given 2D matrix?
DSA C#
int[][] matrix = new int[][] { new int[] {1, 3, 5, 7}, new int[] {10, 11, 16, 20}, new int[] {23, 30, 34, 50} }; int target = 5; bool SearchMatrix(int[][] mat, int t) { int rows = mat.Length; int cols = mat[0].Length; int left = 0, right = rows * cols - 1; while (left <= right) { int mid = (left + right) / 2; int midValue = mat[mid / cols][mid % cols]; if (midValue == t) return true; else if (midValue < t) left = mid + 1; else right = mid - 1; } return false; } Console.WriteLine(SearchMatrix(matrix, target));
Attempts:
2 left
💡 Hint
Think about how the matrix is treated as a sorted list for binary search.
✗ Incorrect
The code treats the 2D matrix as a sorted 1D array by calculating mid index and accessing elements using division and modulo. Since 5 is present in the matrix, the search returns true.
❓ Predict Output
intermediateOutput when target is not in matrix
What will be the output of this C# code searching for target 13 in the 2D matrix?
DSA C#
int[][] matrix = new int[][] { new int[] {1, 3, 5, 7}, new int[] {10, 11, 16, 20}, new int[] {23, 30, 34, 50} }; int target = 13; bool SearchMatrix(int[][] mat, int t) { int rows = mat.Length; int cols = mat[0].Length; int left = 0, right = rows * cols - 1; while (left <= right) { int mid = (left + right) / 2; int midValue = mat[mid / cols][mid % cols]; if (midValue == t) return true; else if (midValue < t) left = mid + 1; else right = mid - 1; } return false; } Console.WriteLine(SearchMatrix(matrix, target));
Attempts:
2 left
💡 Hint
Check if 13 exists in the matrix.
✗ Incorrect
The target 13 is not present in the matrix, so the binary search completes without finding it and returns false.
🔧 Debug
advancedIdentify the error in matrix search code
What error will this C# code produce when searching for target 7 in the matrix?
DSA C#
int[][] matrix = new int[][] { new int[] {1, 2, 3}, new int[] {4, 5, 6} }; int target = 7; bool SearchMatrix(int[][] mat, int t) { int rows = mat.Length; int cols = mat[0].Length; int left = 0, right = rows * cols - 1; while (left <= right) { int mid = (left + right) / 2; int midValue = mat[mid / cols][mid % cols]; if (midValue == t) return true; else if (midValue < t) left = mid + 1; else right = mid - 1; } return false; } Console.WriteLine(SearchMatrix(matrix, target));
Attempts:
2 left
💡 Hint
Check the calculation of right boundary.
✗ Incorrect
The right boundary is set to rows * cols instead of rows * cols - 1, causing mid to go out of range and throw IndexOutOfRangeException.
🧠 Conceptual
advancedWhy binary search works on this 2D matrix
Why can we apply binary search on this 2D matrix as if it were a sorted 1D array?
Attempts:
2 left
💡 Hint
Think about the order of elements across rows.
✗ Incorrect
Binary search works because the matrix is sorted row-wise and each row's first element is greater than the last element of the previous row, making the entire matrix sorted if flattened.
❓ Predict Output
expertOutput of search with duplicate elements in matrix
What is the output of this C# code searching for target 3 in a matrix with duplicates?
DSA C#
int[][] matrix = new int[][] { new int[] {1, 2, 3, 3}, new int[] {4, 5, 6, 7}, new int[] {8, 9, 10, 11} }; int target = 3; bool SearchMatrix(int[][] mat, int t) { int rows = mat.Length; int cols = mat[0].Length; int left = 0, right = rows * cols - 1; while (left <= right) { int mid = (left + right) / 2; int midValue = mat[mid / cols][mid % cols]; if (midValue == t) return true; else if (midValue < t) left = mid + 1; else right = mid - 1; } return false; } Console.WriteLine(SearchMatrix(matrix, target));
Attempts:
2 left
💡 Hint
Duplicates do not affect binary search correctness if matrix is sorted.
✗ Incorrect
The binary search finds the target 3 even if it appears multiple times, returning true.
