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 code when searching for the target 5 in the matrix?
DSA Python
def search_matrix(matrix, target): if not matrix or not matrix[0]: return False rows, cols = len(matrix), len(matrix[0]) r, c = 0, cols - 1 while r < rows and c >= 0: if matrix[r][c] == target: return True elif matrix[r][c] > target: c -= 1 else: r += 1 return False matrix = [ [1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17] ] print(search_matrix(matrix, 5))
Attempts:
2 left
💡 Hint
Think about how the search moves through the matrix starting from the top-right corner.
✗ Incorrect
The function starts at the top-right corner and moves left or down depending on the comparison with the target. Since 5 is in the matrix, it returns True.
❓ Predict Output
intermediateOutput when target is not in matrix
What will the output be when searching for the target 15 in the same matrix?
DSA Python
def search_matrix(matrix, target): if not matrix or not matrix[0]: return False rows, cols = len(matrix), len(matrix[0]) r, c = 0, cols - 1 while r < rows and c >= 0: if matrix[r][c] == target: return True elif matrix[r][c] > target: c -= 1 else: r += 1 return False matrix = [ [1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17] ] print(search_matrix(matrix, 15))
Attempts:
2 left
💡 Hint
Check if 15 exists in the matrix by following the search path.
✗ Incorrect
15 is not present in the matrix, so the function returns False after exhausting possible positions.
🔧 Debug
advancedIdentify the error in this search function
What error does this code raise when searching for target 5?
DSA Python
def search_matrix(matrix, target): rows, cols = len(matrix), len(matrix[0]) r, c = 0, cols while r < rows and c >= 0: if matrix[r][c] == target: return True elif matrix[r][c] > target: c -= 1 else: r += 1 return False matrix = [ [1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17] ] print(search_matrix(matrix, 5))
Attempts:
2 left
💡 Hint
Check the initial value of column index c and valid index range.
✗ Incorrect
The code sets c = cols which is out of range since valid column indices are 0 to cols-1, causing IndexError.
🧠 Conceptual
advancedWhy start search from top-right corner?
Why does the search algorithm for a sorted 2D matrix start from the top-right corner?
Attempts:
2 left
💡 Hint
Think about how the matrix is sorted and how moving left or down changes values.
✗ Incorrect
Starting at top-right allows moving left to smaller values or down to larger values, efficiently narrowing search.
🚀 Application
expertNumber of elements checked in worst case
What is the maximum number of elements checked by the search algorithm in an m x n matrix?
Attempts:
2 left
💡 Hint
Each step moves either one row down or one column left, never revisiting elements.
✗ Incorrect
The search moves at most m times down and n times left, so maximum checks are m + n.
