0
0
DSA C++programming~20 mins

Search in 2D Matrix in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
2D Matrix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output 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++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
    int rows = matrix.size();
    int cols = matrix[0].size();
    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() {
    vector<vector<int>> matrix = {
        {1, 4, 7, 11},
        {2, 5, 8, 12},
        {3, 6, 9, 16},
        {10, 13, 14, 17}
    };
    bool found = searchMatrix(matrix, 5);
    cout << (found ? "Found" : "Not Found") << endl;
    return 0;
}
ARuntime Error
BFound
CCompilation Error
DNot Found
Attempts:
2 left
💡 Hint
Start searching from the top-right corner and move left or down based on comparison.
Predict Output
intermediate
2:00remaining
Output when target is absent in 2D matrix
What will be the output of this C++ code searching for target 15 in the given sorted 2D matrix?
DSA C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
    int rows = matrix.size();
    int cols = matrix[0].size();
    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() {
    vector<vector<int>> matrix = {
        {1, 4, 7, 11},
        {2, 5, 8, 12},
        {3, 6, 9, 16},
        {10, 13, 14, 17}
    };
    bool found = searchMatrix(matrix, 15);
    cout << (found ? "Found" : "Not Found") << endl;
    return 0;
}
ANot Found
BRuntime Error
CCompilation Error
DFound
Attempts:
2 left
💡 Hint
Trace the search path carefully; 15 is not in the matrix.
🔧 Debug
advanced
2:00remaining
Identify the error in 2D matrix search code
What error will this C++ code produce when searching for target 3 in a 2D matrix?
DSA C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
    int rows = matrix.size();
    int cols = matrix[0].size();
    int r = 0, c = cols;
    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() {
    vector<vector<int>> matrix = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    bool found = searchMatrix(matrix, 3);
    cout << (found ? "Found" : "Not Found") << endl;
    return 0;
}
ACompilation Error
BOutput: Not Found
COutput: Found
DRuntime Error: out_of_range
Attempts:
2 left
💡 Hint
Check the initial value of column index c.
Predict Output
advanced
2:00remaining
Output of binary search on flattened 2D matrix
What is the output of this C++ code that uses binary search on a flattened 2D matrix to find target 8?
DSA C++
bool searchMatrix(vector<vector<int>>& matrix, int target) {
    int rows = matrix.size();
    int cols = matrix[0].size();
    int left = 0, right = rows * cols - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        int 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;
    }
    return false;
}

int main() {
    vector<vector<int>> matrix = {
        {1, 3, 5, 7},
        {10, 11, 16, 20},
        {23, 30, 34, 50}
    };
    bool found = searchMatrix(matrix, 8);
    cout << (found ? "Found" : "Not Found") << endl;
    return 0;
}
AFound
BCompilation Error
CNot Found
DRuntime Error
Attempts:
2 left
💡 Hint
8 is not in the matrix; binary search will fail to find it.
🧠 Conceptual
expert
2:00remaining
Time complexity of search in sorted 2D matrix
Given a sorted 2D matrix where each row and column is sorted ascending, what is the time complexity of the search algorithm that starts from the top-right corner and moves left or down?
AO(m + n), where m is number of rows and n is number of columns
BO(m * n), where m is number of rows and n is number of columns
CO(log(m * n)), binary search on flattened matrix
DO(log m + log n), binary search on rows and columns separately
Attempts:
2 left
💡 Hint
Each step moves either one row down or one column left, never revisiting elements.