0
0
DSA Goprogramming~20 mins

Search in 2D Matrix in DSA Go - 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 Go code that searches for the number 5 in a sorted 2D matrix?
DSA Go
package main
import "fmt"
func searchMatrix(matrix [][]int, target int) bool {
    rows := len(matrix)
    if rows == 0 {
        return false
    }
    cols := len(matrix[0])
    r, c := 0, cols-1
    for r < rows && c >= 0 {
        if matrix[r][c] == target {
            return true
        } else if matrix[r][c] > target {
            c--
        } else {
            r++
        }
    }
    return false
}
func main() {
    matrix := [][]int{
        {1, 4, 7, 11},
        {2, 5, 8, 12},
        {3, 6, 9, 16},
        {10, 13, 14, 17},
    }
    fmt.Println(searchMatrix(matrix, 5))
}
Aruntime error
Bfalse
Ctrue
Dcompilation error
Attempts:
2 left
💡 Hint
Think about how the search moves through the matrix starting from the top-right corner.
Predict Output
intermediate
2:00remaining
Output when target is not in matrix
What does the following Go code print when searching for 15 in the given sorted 2D matrix?
DSA Go
package main
import "fmt"
func searchMatrix(matrix [][]int, target int) bool {
    rows := len(matrix)
    if rows == 0 {
        return false
    }
    cols := len(matrix[0])
    r, c := 0, cols-1
    for r < rows && c >= 0 {
        if matrix[r][c] == target {
            return true
        } else if matrix[r][c] > target {
            c--
        } else {
            r++
        }
    }
    return false
}
func main() {
    matrix := [][]int{
        {1, 4, 7, 11},
        {2, 5, 8, 12},
        {3, 6, 9, 16},
        {10, 13, 14, 17},
    }
    fmt.Println(searchMatrix(matrix, 15))
}
Atrue
Bcompilation error
Cruntime error
Dfalse
Attempts:
2 left
💡 Hint
Check if 15 exists in the matrix by following the search path.
🧠 Conceptual
advanced
2:00remaining
Why start search from top-right corner in 2D matrix?
Why is the top-right corner chosen as the starting point for searching a target in a sorted 2D matrix where rows and columns are sorted ascending?
ABecause from top-right, moving left decreases values and moving down increases values, allowing elimination of rows or columns efficiently.
BBecause top-right corner always contains the smallest element in the matrix.
CBecause starting from top-right avoids checking the entire matrix by jumping diagonally.
DBecause top-right corner is the only position where binary search can be applied directly.
Attempts:
2 left
💡 Hint
Think about how the values change when moving left or down from the top-right corner.
🔧 Debug
advanced
2:00remaining
Identify the bug in 2D matrix search code
What error will the following Go code produce when searching for 3 in the matrix, and why?
DSA Go
package main
import "fmt"
func searchMatrix(matrix [][]int, target int) bool {
    rows := len(matrix)
    if rows == 0 {
        return false
    }
    cols := len(matrix[0])
    r, c := 0, cols-1
    for r < rows && c >= 0 {
        if matrix[r][c] == target {
            return true
        } else if matrix[r][c] > target {
            c--
        } else {
            r++
        }
    }
    return false
}
func main() {
    matrix := [][]int{
        {1, 4, 7},
        {2, 5, 8},
        {3, 6, 9},
    }
    fmt.Println(searchMatrix(matrix, 3))
}
Aruntime error: index out of range
Bfalse
Ctrue
Dcompilation error
Attempts:
2 left
💡 Hint
Check the initial value of column index c and how it is used to access matrix elements.
🚀 Application
expert
3:00remaining
Count occurrences of target in sorted 2D matrix
Given a sorted 2D matrix where each row and column is sorted ascending, which approach efficiently counts how many times a target appears?
ATraverse entire matrix and count occurrences, resulting in O(m*n) time.
BStart from bottom-left corner and move up or right to count all occurrences in O(m+n) time.
CPerform binary search on each row separately and sum counts, resulting in O(m log n) time.
DStart from top-left corner and move diagonally down-right to count occurrences.
Attempts:
2 left
💡 Hint
Consider how moving up or right from bottom-left corner helps eliminate rows or columns.