Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the target is found in the matrix.
DSA C
if(matrix[i][j] == [1]) { return 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with the wrong variable like i or j instead of target.
Using matrix instead of target for comparison.
✗ Incorrect
We compare the current element matrix[i][j] with the target value to find a match.
2fill in blank
mediumComplete the code to iterate over rows of the matrix.
DSA C
for(int i = 0; i < [1]; i++) { // inner loop }
Attempts:
3 left
💡 Hint
Common Mistakes
Using cols instead of rows for the outer loop.
Using target or matrix as loop limits.
✗ Incorrect
The outer loop should run over the number of rows in the matrix.
3fill in blank
hardFix the error in the inner loop to iterate over columns.
DSA C
for(int j = 0; j < [1]; j++) { // check element }
Attempts:
3 left
💡 Hint
Common Mistakes
Using rows instead of cols for the inner loop.
Using target or matrix as loop limits.
✗ Incorrect
The inner loop should run over the number of columns in the matrix.
4fill in blank
hardFill both blanks to complete the function signature and return type.
DSA C
[1] searchMatrix(int** matrix, int rows, int cols, int target) { for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { if(matrix[i][j] == target) { return [2]; } } } return 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type but returning a value.
Returning true instead of 1 in C.
✗ Incorrect
The function returns an int indicating if the target is found (1) or not (0).
5fill in blank
hardFill all three blanks to complete the optimized search in a sorted 2D matrix.
DSA C
int searchMatrix(int** matrix, int rows, int cols, int target) {
int row = 0, col = [1] - 1;
while(row < rows && col >= 0) {
if(matrix[row][col] == target) {
return [2];
} else if(matrix[row][col] > target) {
col[3];
} else {
row++;
}
}
return 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using rows instead of cols for column index initialization.
Returning 0 instead of 1 when target is found.
Incrementing column instead of decrementing when current > target.
✗ Incorrect
Start from top-right corner (col = cols - 1), return 1 if found, move left (col--) if current > target.
