Challenge - 5 Problems
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 a sorted 2D matrix?
DSA C
#include <stdio.h> #include <stdbool.h> bool searchMatrix(int matrix[][4], int rows, int cols, int target) { 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; } int main() { int matrix[3][4] = { {1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60} }; bool found = searchMatrix(matrix, 3, 4, 5); printf("%s\n", found ? "true" : "false"); return 0; }
Attempts:
2 left
💡 Hint
Trace the search starting from top-right corner comparing with target 5.
✗ Incorrect
The code starts at the top-right element (7). Since 7 > 5, it moves left to 5, which matches the target. So it returns true.
❓ Predict Output
intermediateOutput when target is not in matrix
What does the following C code print when searching for target 13 in the given 2D matrix?
DSA C
#include <stdio.h> #include <stdbool.h> bool searchMatrix(int matrix[][3], int rows, int cols, int target) { 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; } int main() { int matrix[3][3] = { {1, 4, 7}, {8, 10, 12}, {13, 14, 16} }; bool found = searchMatrix(matrix, 3, 3, 13); printf("%s\n", found ? "true" : "false"); return 0; }
Attempts:
2 left
💡 Hint
Check if 13 exists in the matrix.
✗ Incorrect
13 is present at matrix[2][0], so the function returns true and prints "true".
❓ Predict Output
advancedOutput of search in non-square matrix
What is the output of this C code searching for target 15 in a 3x5 matrix?
DSA C
#include <stdio.h> #include <stdbool.h> bool searchMatrix(int matrix[][5], int rows, int cols, int target) { 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; } int main() { int matrix[3][5] = { {1, 3, 5, 7, 9}, {10, 11, 12, 13, 14}, {15, 16, 17, 18, 19} }; bool found = searchMatrix(matrix, 3, 5, 15); printf("%s\n", found ? "true" : "false"); return 0; }
Attempts:
2 left
💡 Hint
Look for 15 in the last row, first column.
✗ Incorrect
15 is found at matrix[2][0], so the function returns true and prints "true".
❓ Predict Output
advancedOutput when target is smaller than all elements
What does this C code print when searching for target 0 in the matrix?
DSA C
#include <stdio.h> #include <stdbool.h> bool searchMatrix(int matrix[][3], int rows, int cols, int target) { 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; } int main() { int matrix[3][3] = { {5, 6, 7}, {8, 9, 10}, {11, 12, 13} }; bool found = searchMatrix(matrix, 3, 3, 0); printf("%s\n", found ? "true" : "false"); return 0; }
Attempts:
2 left
💡 Hint
Target 0 is less than all matrix elements.
✗ Incorrect
Since 0 is smaller than all elements, the search moves left until column < 0 and returns false.
🧠 Conceptual
expertTime complexity of search in sorted 2D matrix
What is the time complexity of searching a target in an m x n matrix where each row and column is sorted ascending, using the top-right corner search method?
Attempts:
2 left
💡 Hint
Each step moves either one row down or one column left.
✗ Incorrect
The search starts at top-right and moves at most m times down and n times left, so total steps are O(m + n).
