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 binary search in 2D matrix
What is the output of the following Rust code that searches for the target 5 in a sorted 2D matrix?
DSA Rust
fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool { if matrix.is_empty() || matrix[0].is_empty() { return false; } let rows = matrix.len(); let cols = matrix[0].len(); let mut left = 0; let mut right = rows * cols - 1; while left <= right { let mid = (left + right) / 2; let mid_value = matrix[mid / cols][mid % cols]; if mid_value == target { return true; } else if mid_value < target { left = mid + 1; } else { right = mid - 1; } } false } fn main() { let matrix = vec![ vec![1, 3, 5, 7], vec![10, 11, 16, 20], vec![23, 30, 34, 50], ]; println!("{}", search_matrix(matrix, 5)); }
Attempts:
2 left
💡 Hint
Think about how the matrix is treated as a sorted list in the binary search.
✗ Incorrect
The code treats the 2D matrix as a sorted 1D array by calculating mid index and accessing elements accordingly. Since 5 is present, it returns true.
❓ Predict Output
intermediateOutput when target is not in matrix
What will the output be when searching for target 13 in the same matrix using the given Rust function?
DSA Rust
fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool { if matrix.is_empty() || matrix[0].is_empty() { return false; } let rows = matrix.len(); let cols = matrix[0].len(); let mut left = 0; let mut right = rows * cols - 1; while left <= right { let mid = (left + right) / 2; let mid_value = matrix[mid / cols][mid % cols]; if mid_value == target { return true; } else if mid_value < target { left = mid + 1; } else { right = mid - 1; } } false } fn main() { let matrix = vec![ vec![1, 3, 5, 7], vec![10, 11, 16, 20], vec![23, 30, 34, 50], ]; println!("{}", search_matrix(matrix, 13)); }
Attempts:
2 left
💡 Hint
Check if 13 exists in the matrix.
✗ Incorrect
Since 13 is not present in the matrix, the function returns false after exhausting the search.
🧠 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 ordering of elements across rows.
✗ Incorrect
The matrix is sorted such that each row is sorted and the first element of a row is greater than the last element of the previous row, allowing binary search treating it as a sorted list.
❓ Predict Output
advancedOutput of search with empty matrix
What is the output of the following Rust code when searching for any target in an empty matrix?
DSA Rust
fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool { if matrix.is_empty() || matrix[0].is_empty() { return false; } let rows = matrix.len(); let cols = matrix[0].len(); let mut left = 0; let mut right = rows * cols - 1; while left <= right { let mid = (left + right) / 2; let mid_value = matrix[mid / cols][mid % cols]; if mid_value == target { return true; } else if mid_value < target { left = mid + 1; } else { right = mid - 1; } } false } fn main() { let matrix: Vec<Vec<i32>> = vec![]; println!("{}", search_matrix(matrix, 1)); }
Attempts:
2 left
💡 Hint
Check the initial condition for empty matrix.
✗ Incorrect
The function returns false immediately because the matrix is empty and has no elements to search.
❓ Predict Output
expertOutput of search with matrix containing duplicates
What is the output of the following Rust code when searching for target 3 in a matrix that contains duplicates?
DSA Rust
fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool { if matrix.is_empty() || matrix[0].is_empty() { return false; } let rows = matrix.len(); let cols = matrix[0].len(); let mut left = 0; let mut right = rows * cols - 1; while left <= right { let mid = (left + right) / 2; let mid_value = matrix[mid / cols][mid % cols]; if mid_value == target { return true; } else if mid_value < target { left = mid + 1; } else { right = mid - 1; } } false } fn main() { let matrix = vec![ vec![1, 3, 3, 7], vec![10, 11, 16, 20], vec![23, 30, 34, 50], ]; println!("{}", search_matrix(matrix, 3)); }
Attempts:
2 left
💡 Hint
Duplicates do not affect the binary search if the matrix is sorted properly.
✗ Incorrect
The function returns true because 3 is present in the matrix, even if duplicated.
