0
0
DSA Goprogramming~10 mins

Search in 2D Matrix in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the matrix is empty.

DSA Go
if len(matrix) == [1] {
    return false
}
Drag options to blanks, or click blank then click option'
A1
B-1
C0
Dlen(matrix[0])
Attempts:
3 left
💡 Hint
Common Mistakes
Checking len(matrix[0]) without verifying matrix is not empty.
Using 1 instead of 0 for empty check.
2fill in blank
medium

Complete the code to get the number of columns in the matrix.

DSA Go
cols := len(matrix[1])
Drag options to blanks, or click blank then click option'
A{0}
B0
C(0)
D[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of square brackets.
Using len(matrix) instead of len(matrix[0]) for columns.
3fill in blank
hard

Fix the error in the binary search condition to find the target in a row.

DSA Go
for left <= right {
    mid := left + (right - left) / 2
    if matrix[row][mid] == [1] {
        return true
    } else if matrix[row][mid] < target {
        left = mid + 1
    } else {
        right = mid - 1
    }
}
Drag options to blanks, or click blank then click option'
Amid
Btarget
Crow
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with mid index instead of target value.
Using wrong variable names in condition.
4fill in blank
hard

Fill both blanks to correctly perform binary search on the matrix rows.

DSA Go
for left, right := 0, [1] - 1; left <= right; {
    mid := left + (right - left) / 2
    if matrix[mid][0] == target {
        return true
    } else if matrix[mid][0] < [2] {
        left = mid + 1
    } else {
        right = mid - 1
    }
}
Drag options to blanks, or click blank then click option'
Alen(matrix)
Btarget
Clen(matrix[0])
Dmid
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(matrix[0]) for rows count.
Comparing with mid index instead of target.
5fill in blank
hard

Fill all three blanks to complete the function that searches target in the 2D matrix.

DSA Go
func searchMatrix(matrix [][]int, target int) bool {
    if len(matrix) == 0 || len(matrix[1]) == 0 {
        return false
    }
    rows, cols := len(matrix), len(matrix[0])
    left, right := 0, rows - 1
    for left <= right {
        mid := left + (right - left) / 2
        if matrix[mid][0] == [2] {
            return true
        } else if matrix[mid][0] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    row := right
    if row < 0 {
        return false
    }
    left, right = 0, cols - 1
    for left <= right {
        mid := left + (right - left) / 2
        if matrix[row][mid] == target {
            return true
        } else if matrix[row][mid] < [3] {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return false
}
Drag options to blanks, or click blank then click option'
A[0]
Btarget
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index or variable in comparisons.
Not checking if matrix[0] exists before accessing.